parse root config and pass to wireguard

This commit is contained in:
octeep 2022-03-10 17:24:48 +00:00
parent 07a7c8f592
commit eeb62aaf46

102
main.go
View file

@ -1,16 +1,17 @@
package main package main
import ( import (
"io"
"log"
"net/http"
"fmt"
"os"
"bufio" "bufio"
"strings"
"errors"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strings"
"golang.zx2c4.com/go118/netip" "golang.zx2c4.com/go118/netip"
"golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/conn"
@ -43,7 +44,7 @@ func readConfig(path string) (Configuration, error) {
defer file.Close() defer file.Close()
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
section := ConfigSection{ name: "ROOT", entries: map[string]string{} } section := ConfigSection{name: "ROOT", entries: map[string]string{}}
sections := []ConfigSection{} sections := []ConfigSection{}
lineNo := 0 lineNo := 0
@ -64,7 +65,7 @@ func readConfig(path string) (Configuration, error) {
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
sections = append(sections, section) 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 continue
} }
@ -102,6 +103,63 @@ func parseBase64Key(key string) (string, error) {
return hex.EncodeToString(decoded), nil 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() { func main() {
fmt.Println("hi") fmt.Println("hi")
conf, err := readConfig("/home/octeep/.config/wireproxy") conf, err := readConfig("/home/octeep/.config/wireproxy")
@ -113,37 +171,23 @@ func main() {
fmt.Println(section.name) fmt.Println(section.name)
} }
root := configRoot(conf) request, dns, err := createIPCRequest(conf)
peerPK, err := parseBase64Key(root["peerpublickey"])
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
selfSK, err := parseBase64Key(root["selfsecretkey"]) test(request, dns)
if err != nil {
log.Panic(err)
}
fmt.Println(peerPK)
fmt.Println(selfSK)
fmt.Println(root)
} }
func test() { func test(request string, dns []netip.Addr) {
tun, tnet, err := netstack.CreateNetTUN( tun, tnet, err := netstack.CreateNetTUN(
[]netip.Addr{netip.MustParseAddr("192.168.4.29")}, []netip.Addr{netip.MustParseAddr("172.16.31.2")},
[]netip.Addr{netip.MustParseAddr("8.8.8.8")}, dns, 1420)
1420)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(device.LogLevelVerbose, "")) dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(device.LogLevelVerbose, ""))
dev.IpcSet(`private_key=a8dac1d8a70a751f0f699fb14ba1cff7b79cf4fbd8f09f44c6e6a90d0369604f dev.IpcSet(request)
public_key=25123c5dcd3328ff645e4f2a3fce0d754400d3887a0cb7c56f0267e20fbf3c5b
endpoint=163.172.161.0:12912
allowed_ip=0.0.0.0/0
`)
err = dev.Up() err = dev.Up()
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)