diff --git a/main.go b/main.go index f78f534..4b07fef 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,17 @@ package main import ( - "io" - "log" - "net/http" - "fmt" - "os" "bufio" - "strings" - "errors" "encoding/base64" "encoding/hex" + "errors" + "fmt" + "io" + "log" + "net" + "net/http" + "os" + "strings" "golang.zx2c4.com/go118/netip" "golang.zx2c4.com/wireguard/conn" @@ -43,7 +44,7 @@ func readConfig(path string) (Configuration, error) { defer file.Close() scanner := bufio.NewScanner(file) - section := ConfigSection{ name: "ROOT", entries: map[string]string{} } + section := ConfigSection{name: "ROOT", entries: map[string]string{}} sections := []ConfigSection{} lineNo := 0 @@ -64,7 +65,7 @@ func readConfig(path string) (Configuration, error) { if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { sections = append(sections, section) - section = ConfigSection{ name: strings.ToLower(line), entries: map[string]string{} } + section = ConfigSection{name: strings.ToLower(line), entries: map[string]string{}} continue } @@ -102,6 +103,63 @@ func parseBase64Key(key string) (string, error) { return hex.EncodeToString(decoded), nil } +func resolveIP(ip string) (*net.IPAddr, error) { + return net.ResolveIPAddr("ip", ip) +} + +func resolveIPPAndPort(addr string) (string, error) { + host, port, err := net.SplitHostPort(addr) + if err != nil { + return "", err + } + + ip, err := resolveIP(host) + if err != nil { + return "", err + } + return net.JoinHostPort(ip.String(), port), nil +} + +func parseIPs(s string) ([]netip.Addr, error) { + ips := []netip.Addr{} + for _, str := range strings.Split(s, ",") { + str = strings.TrimSpace(str) + ip, err := netip.ParseAddr(str) + if err != nil { + return nil, err + } + ips = append(ips, ip) + } + return ips, nil +} + +func createIPCRequest(conf Configuration) (string, []netip.Addr, error) { + root := configRoot(conf) + + peerPK, err := parseBase64Key(root["peerpublickey"]) + if err != nil { + return "", nil, err + } + + selfSK, err := parseBase64Key(root["selfsecretkey"]) + if err != nil { + return "", nil, err + } + + endpoint, err := resolveIPPAndPort(root["peerendpoint"]) + if err != nil { + return "", nil, err + } + + dns, err := parseIPs(root["dns"]) + if err != nil { + return "", nil, err + } + + request := fmt.Sprintf("private_key=%s\npublic_key=%s\nendpoint=%s\nallowed_ip=0.0.0.0/0\n", selfSK, peerPK, endpoint) + return request, dns, nil +} + func main() { fmt.Println("hi") conf, err := readConfig("/home/octeep/.config/wireproxy") @@ -113,37 +171,23 @@ func main() { fmt.Println(section.name) } - root := configRoot(conf) - - peerPK, err := parseBase64Key(root["peerpublickey"]) + request, dns, err := createIPCRequest(conf) if err != nil { log.Panic(err) } - selfSK, err := parseBase64Key(root["selfsecretkey"]) - if err != nil { - log.Panic(err) - } - - fmt.Println(peerPK) - fmt.Println(selfSK) - fmt.Println(root) + test(request, dns) } -func test() { +func test(request string, dns []netip.Addr) { tun, tnet, err := netstack.CreateNetTUN( - []netip.Addr{netip.MustParseAddr("192.168.4.29")}, - []netip.Addr{netip.MustParseAddr("8.8.8.8")}, - 1420) + []netip.Addr{netip.MustParseAddr("172.16.31.2")}, + dns, 1420) if err != nil { log.Panic(err) } dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(device.LogLevelVerbose, "")) - dev.IpcSet(`private_key=a8dac1d8a70a751f0f699fb14ba1cff7b79cf4fbd8f09f44c6e6a90d0369604f -public_key=25123c5dcd3328ff645e4f2a3fce0d754400d3887a0cb7c56f0267e20fbf3c5b -endpoint=163.172.161.0:12912 -allowed_ip=0.0.0.0/0 -`) + dev.IpcSet(request) err = dev.Up() if err != nil { log.Panic(err)