Browse Source

feat: add `logrus` for log

v1.x.x
dnomd343 2 years ago
parent
commit
72464cf30a
  1. 6
      go.mod
  2. 10
      go.sum
  3. 18
      src/config.go
  4. 16
      src/main.go

6
go.mod

@ -2,4 +2,8 @@ module XProxy
go 1.18
require gopkg.in/yaml.v3 v3.0.1 // indirect
require (
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

10
go.sum

@ -1,3 +1,13 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

18
src/config.go

@ -1,6 +1,7 @@
package main
import (
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"net"
"strconv"
@ -66,10 +67,12 @@ func isIPv6(ipAddr string, isRange bool) bool {
func loadConfig(rawConfig []byte) {
config := Config{}
log.Debug("Decode yaml content -> \n", string(rawConfig))
err := yaml.Unmarshal(rawConfig, &config) // yaml (or json) decode
if err != nil {
panic(err)
}
log.Debug("Decoded config -> ", config)
for _, address := range config.Network.DNS { // dns options
if isIPv4(address, false) || isIPv6(address, false) {
@ -78,6 +81,7 @@ func loadConfig(rawConfig []byte) {
panic("Invalid DNS server -> " + address)
}
}
log.Info("DNS server -> ", dnsServer)
for _, address := range config.Network.ByPass { // bypass options
if isIPv4(address, true) {
@ -88,24 +92,30 @@ func loadConfig(rawConfig []byte) {
panic("Invalid bypass CIDR -> " + address)
}
}
log.Info("IPv4 bypass CIDR -> ", v4Bypass)
log.Info("IPv6 bypass CIDR -> ", v6Bypass)
v4Address = config.Network.IPv4.Address // ipv4 options
v4Gateway = config.Network.IPv4.Gateway
v4Forward = config.Network.IPv4.Forward
v6Forward = config.Network.IPv6.Forward
log.Infof("IP forward -> IPv4 = %v | IPv6 = %v", v4Forward, v6Forward)
v4Address = config.Network.IPv4.Address
v4Gateway = config.Network.IPv4.Gateway
if v4Address != "" && !isIPv4(v4Address, true) {
panic("Invalid IPv4 address -> " + v4Address)
}
if v4Gateway != "" && !isIPv4(v4Gateway, false) {
panic("Invalid IPv4 gateway -> " + v4Gateway)
}
log.Infof("IPv4 -> address = %s | gateway = %s", v4Address, v4Gateway)
v6Address = config.Network.IPv6.Address // ipv6 options
v6Address = config.Network.IPv6.Address
v6Gateway = config.Network.IPv6.Gateway
v6Forward = config.Network.IPv6.Forward
if v6Address != "" && !isIPv6(v6Address, true) {
panic("Invalid IPv6 address -> " + v6Address)
}
if v6Gateway != "" && !isIPv6(v6Gateway, false) {
panic("Invalid IPv6 gateway -> " + v6Gateway)
}
log.Infof("IPv6 -> address = %s | gateway = %s", v6Address, v6Gateway)
}

16
src/main.go

@ -1,26 +1,16 @@
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
)
func main() {
fmt.Println("XProxy start")
log.SetLevel(log.DebugLevel)
log.Warning("XProxy start")
content, err := os.ReadFile("test.yml")
if err != nil {
panic(err)
}
loadConfig(content)
fmt.Println("DNS ->", dnsServer)
fmt.Println("v4Bypass ->", v4Bypass)
fmt.Println("v6Bypass ->", v6Bypass)
fmt.Println("v4Gateway ->", v4Gateway)
fmt.Println("v4Address ->", v4Address)
fmt.Println("v6Gateway ->", v6Gateway)
fmt.Println("v6Address ->", v6Address)
fmt.Println("v4Forward ->", v4Forward)
fmt.Println("v6Forward ->", v6Forward)
}

Loading…
Cancel
Save