simpleproxy/main.go

65 lines
1.3 KiB
Go
Raw Normal View History

2024-08-01 23:52:37 +01:00
package main
import (
2025-02-04 17:05:54 +00:00
"context"
"flag"
2024-08-01 23:52:37 +01:00
"log"
"os"
2025-02-04 17:05:54 +00:00
"os/signal"
2024-08-01 23:52:37 +01:00
"strings"
2025-02-04 17:05:54 +00:00
"sync"
"syscall"
2024-08-01 23:52:37 +01:00
)
func main() {
2025-02-04 17:05:54 +00:00
flag.Parse()
configPath := os.Getenv("GOPROXY_CONFIG")
if configPath == "" || flag.Arg(0) == "" {
configPath = "goproxy.json" // Default path for Docker
2024-08-01 23:52:37 +01:00
}
2025-02-04 17:13:27 +00:00
config := ReadConfig(configPath)
2024-08-01 23:52:37 +01:00
2025-02-04 17:05:54 +00:00
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
2024-08-01 23:52:37 +01:00
for _, proxy := range config.Proxy {
if proxy.Type == "" {
proxy.Type = "both"
}
if proxy.Local == "" {
parts := strings.Split(proxy.Remote, ":")
if len(parts) == 2 {
proxy.Local = ":" + parts[1]
} else {
2025-02-04 17:05:54 +00:00
log.Fatalf("[ERROR] Invalid remote address format: %s\n", proxy.Remote)
2024-08-01 23:52:37 +01:00
}
}
2025-02-04 17:05:54 +00:00
wg.Add(1)
2024-08-01 23:52:37 +01:00
switch proxy.Type {
case "tcp":
2025-02-04 17:05:54 +00:00
go startTCPProxy(ctx, &wg, proxy.Local, proxy.Remote)
2024-08-01 23:52:37 +01:00
case "udp":
2025-02-04 17:05:54 +00:00
go startUDPProxy(ctx, &wg, proxy.Local, proxy.Remote)
2024-08-01 23:52:37 +01:00
case "both":
2025-02-04 17:05:54 +00:00
go startTCPProxy(ctx, &wg, proxy.Local, proxy.Remote)
go startUDPProxy(ctx, &wg, proxy.Local, proxy.Remote)
2024-08-01 23:52:37 +01:00
default:
2025-02-04 17:05:54 +00:00
log.Printf("[WARNING] Unknown proxy type: %s\n", proxy.Type)
wg.Done()
2024-08-01 23:52:37 +01:00
}
}
2025-02-04 17:05:54 +00:00
// Handle termination signals
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
log.Println("[INFO] Shutting down proxy server...")
cancel()
wg.Wait()
log.Println("[INFO] Proxy server stopped.")
2024-08-01 23:52:37 +01:00
}