diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..dee76c5 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module XProxy + +go 1.18 + +require gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4bc0337 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/config.go b/src/config.go new file mode 100644 index 0000000..d36dd81 --- /dev/null +++ b/src/config.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "gopkg.in/yaml.v3" + "net" + "strconv" + "strings" +) + +type netConfig struct { + Gateway string `yaml:"gateway"` // network gateway + Address string `yaml:"address"` // network address + Forward bool `yaml:"forward"` // enabled net forward +} + +type Config struct { + Network struct { + DNS []string `yaml:"dns"` // system dns server + ByPass []string `yaml:"bypass"` // cidr bypass list + IPv4 netConfig `yaml:"ipv4"` // ipv4 network configure + IPv6 netConfig `yaml:"ipv6"` // ipv6 network configure + } +} + +func isIP(ipAddr string, isRange bool, allowEmpty bool, ipLength int, ipFlag string) bool { + var address string + if allowEmpty && ipAddr == "" { // empty case + return true + } + if isRange { + temp := strings.Split(ipAddr, "/") + if len(temp) != 2 { // not {IP_ADDRESS}/{LENGTH} format + return false + } + length, err := strconv.Atoi(temp[1]) + if err != nil { // range length not a integer + return false + } + if length < 0 || length > ipLength { // length should between 0 ~ ipLength + return false + } + address = temp[0] + } else { + address = ipAddr + } + ip := net.ParseIP(address) // try to convert ip + return ip != nil && strings.Contains(address, ipFlag) +} + +func isIPv4(ipAddr string, isRange bool, allowEmpty bool) bool { + return isIP(ipAddr, isRange, allowEmpty, 32, ".") +} + +func isIPv6(ipAddr string, isRange bool, allowEmpty bool) bool { + return isIP(ipAddr, isRange, allowEmpty, 128, ":") +} + +func loadConfig(rawConfig []byte) { + config := Config{} + err := yaml.Unmarshal(rawConfig, &config) // yaml (or json) decode + if err != nil { + panic(err) + } + fmt.Println(config) +} diff --git a/src/main.go b/src/main.go index 250fddf..e27dedb 100644 --- a/src/main.go +++ b/src/main.go @@ -2,52 +2,27 @@ package main import ( "fmt" - "net" - "strconv" - "strings" ) -func isIP(ipAddr string, isRange bool, allowEmpty bool, ipLength int, ipFlag string) bool { - var address string - if allowEmpty && ipAddr == "" { // empty case - return true - } - if isRange { - temp := strings.Split(ipAddr, "/") - if len(temp) != 2 { // not {IP_ADDRESS}/{LENGTH} format - return false - } - length, err := strconv.Atoi(temp[1]) - if err != nil { // range length not a integer - return false - } - if length < 0 || length > ipLength { // length should between 0 ~ ipLength - return false - } - address = temp[0] - } else { - address = ipAddr - } - ip := net.ParseIP(address) // try to convert ip - return ip != nil && strings.Contains(address, ipFlag) -} - -func isIPv4(ipAddr string, isRange bool, allowEmpty bool) bool { - return isIP(ipAddr, isRange, allowEmpty, 32, ".") -} - -func isIPv6(ipAddr string, isRange bool, allowEmpty bool) bool { - return isIP(ipAddr, isRange, allowEmpty, 128, ":") -} - func main() { - fmt.Println("XProxy") + fmt.Println("XProxy start") + + yamlContent := []byte(` +network: + dns: # system's dns server + - 223.5.5.5 + - 119.29.29.29 +# - fesdc.fardf.afa + ipv4: # ipv4 network configure + gateway: 192.168.2.1 + address: 192.168.2.2/24 + forward: false + ipv6: null + bypass: + - 169.254.0.0/16 + - fc00::/7 + - 224.0.0.0/3 +`) - fmt.Println(isIPv4("1.1.1.1", false, false)) - fmt.Println(isIPv4("1.1.1.1/24", true, false)) - fmt.Println(isIPv4("", true, true)) - fmt.Println(isIPv4("::1", true, true)) - fmt.Println(isIPv6("::1", true, true)) - fmt.Println(isIPv6("::1/32", true, true)) - fmt.Println(isIPv6("::1", true, true)) + loadConfig(yamlContent) }