diff --git a/flora/config.go b/flora/config.go index 88f3bc3..1bead1f 100644 --- a/flora/config.go +++ b/flora/config.go @@ -2,14 +2,15 @@ package flora import ( "fmt" - "github.com/go-ini/ini" - ss "github.com/shadowsocks/shadowsocks-go/shadowsocks" "log" + "math/rand" "net" "os" "strconv" "strings" - "math/rand" + + "github.com/go-ini/ini" + ss "github.com/shadowsocks/shadowsocks-go/shadowsocks" ) const ( @@ -38,7 +39,7 @@ type proxyGroup struct { proxyServers []ProxyServer } -func LoadConfig(cfgFile string, geoFile string) (*ProxyConfig) { +func LoadConfig(cfgFile string, geoFile string) *ProxyConfig { proxyConfig := ProxyConfig{} var iniOpts = ini.LoadOptions{ AllowBooleanKeys: true, @@ -148,7 +149,7 @@ func loadProxy(config *ProxyConfig) { } -func (c *ProxyConfig) GetProxyServer(action string) (ProxyServer) { +func (c *ProxyConfig) GetProxyServer(action string) ProxyServer { const maxFailCnt = 30 var server ProxyServer var ok bool diff --git a/flora/geoip.go b/flora/geoip.go index 0d00f80..58cfac9 100644 --- a/flora/geoip.go +++ b/flora/geoip.go @@ -1,10 +1,11 @@ package flora import ( - "github.com/oschwald/geoip2-golang" "log" "net" "strings" + + "github.com/oschwald/geoip2-golang" ) var geoDB *geoip2.Reader diff --git a/flora/http.go b/flora/http.go index e71f2b8..4224de6 100644 --- a/flora/http.go +++ b/flora/http.go @@ -1,11 +1,11 @@ package flora import ( - "net" - "io" - "net/http" "bufio" "bytes" + "io" + "net" + "net/http" "net/http/httputil" ) @@ -50,8 +50,8 @@ func httpProxyConnect(conn net.Conn, first byte) (addr string, hostType int, raw return } -func getRequestType(addr string) int { - host,_,_ := net.SplitHostPort(addr) +func getRequestType(addr string) int { + host, _, _ := net.SplitHostPort(addr) ip := net.ParseIP(host) if nil != ip { return typeIPv4 @@ -59,7 +59,6 @@ func getRequestType(addr string) int { return typeDm } - func removeHeaders(req *http.Request) { req.RequestURI = "" req.Header.Del("Accept-Encoding") diff --git a/flora/network_setup.go b/flora/network_setup.go index 7b696e5..2468d31 100644 --- a/flora/network_setup.go +++ b/flora/network_setup.go @@ -1,11 +1,11 @@ package flora import ( - "os/signal" - "syscall" - "os" "log" + "os" + "os/signal" "runtime" + "syscall" "time" ) @@ -21,7 +21,7 @@ func resetProxySettings(proxySettings SystemProxySettings) { select { case <-sigs: log.Print("Flora-kit is shutdown now ...") - if nil != proxySettings{ + if nil != proxySettings { proxySettings.TurnOffGlobProxy() } time.Sleep(time.Duration(2000)) @@ -30,17 +30,17 @@ func resetProxySettings(proxySettings SystemProxySettings) { } } -func initProxySettings(bypass []string, addr string) { +func initProxySettings(bypass []string, addr string) { signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) var proxySettings SystemProxySettings if runtime.GOOS == "windows" { w := &windows{addr} proxySettings = w } else if runtime.GOOS == "darwin" { - d := &darwin{bypass,addr} + d := &darwin{bypass, addr} proxySettings = d } - if nil != proxySettings{ + if nil != proxySettings { proxySettings.TurnOnGlobProxy() } go resetProxySettings(proxySettings) diff --git a/flora/network_setup_mac.go b/flora/network_setup_mac.go index b5476c0..0f1f2a1 100644 --- a/flora/network_setup_mac.go +++ b/flora/network_setup_mac.go @@ -3,9 +3,9 @@ package flora import ( "bytes" "log" + "net" "os/exec" "strings" - "net" ) type darwin struct { diff --git a/flora/network_setup_win.go b/flora/network_setup_win.go index a1c1828..6a15224 100644 --- a/flora/network_setup_win.go +++ b/flora/network_setup_win.go @@ -1,8 +1,8 @@ package flora import ( - "os/exec" "log" + "os/exec" ) type windows struct { @@ -10,13 +10,13 @@ type windows struct { } const ( - cmdRegistry = `reg` - cmdRegistryAdd = `add` - internetSettingsKey = `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings` - keyProxyEnable = `ProxyEnable` - keyProxyServer = `ProxyServer` - dataTypeDWord = `REG_DWORD` - dataTypeRegSZ = `REG_SZ` + cmdRegistry = `reg` + cmdRegistryAdd = `add` + internetSettingsKey = `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings` + keyProxyEnable = `ProxyEnable` + keyProxyServer = `ProxyServer` + dataTypeDWord = `REG_DWORD` + dataTypeRegSZ = `REG_SZ` ) func (w *windows) TurnOnGlobProxy() { @@ -41,7 +41,7 @@ func (w *windows) TurnOffGlobProxy() { if _, err = c.CombinedOutput(); err != nil { log.Printf("disable windows proxy has error %s", err) } - if nil == err{ + if nil == err { log.Print("disable windows proxy settings are successful ...") } } diff --git a/flora/proxy_direct.go b/flora/proxy_direct.go index 72edf5a..1b314e0 100644 --- a/flora/proxy_direct.go +++ b/flora/proxy_direct.go @@ -6,7 +6,7 @@ type DirectServer struct { proxyType string } -func NewDirect() (*DirectServer) { +func NewDirect() *DirectServer { return &DirectServer{proxyType: ServerTypeDirect} } @@ -14,7 +14,7 @@ func (s *DirectServer) FailCount() int { return 0 } -func (s *DirectServer) ResetFailCount() { +func (s *DirectServer) ResetFailCount() { } @@ -28,10 +28,10 @@ func (s *DirectServer) ProxyType() string { func (s *DirectServer) DialWithRawAddr(raw []byte, host string) (remote net.Conn, err error) { conn, err := net.Dial("tcp", host) - if nil != err{ - return nil,err + if nil != err { + return nil, err } - if nil != raw && len(raw) > 0 { + if nil != raw && len(raw) > 0 { conn.Write(raw) } return conn, err diff --git a/flora/proxy_reject.go b/flora/proxy_reject.go index c58a195..6508c28 100644 --- a/flora/proxy_reject.go +++ b/flora/proxy_reject.go @@ -6,7 +6,7 @@ type Reject struct { proxyType string } -func NewReject() (*Reject) { +func NewReject() *Reject { return &Reject{proxyType: ServerTypeReject} } diff --git a/flora/proxy_shadowsocks.go b/flora/proxy_shadowsocks.go index 2bbae94..dd75419 100644 --- a/flora/proxy_shadowsocks.go +++ b/flora/proxy_shadowsocks.go @@ -1,8 +1,8 @@ package flora import ( - "net" ss "github.com/shadowsocks/shadowsocks-go/shadowsocks" + "net" "sync" ) @@ -14,7 +14,7 @@ type ShadowSocksServer struct { lock sync.RWMutex } -func NewShadowSocks(server string, cipher *ss.Cipher) (*ShadowSocksServer) { +func NewShadowSocks(server string, cipher *ss.Cipher) *ShadowSocksServer { return &ShadowSocksServer{ proxyType: ServerTypeShadowSocks, server: server, @@ -29,7 +29,7 @@ func (s *ShadowSocksServer) ResetFailCount() { } func (s *ShadowSocksServer) AddFail() { - s.failCount ++ + s.failCount++ } func (s *ShadowSocksServer) FailCount() int { diff --git a/flora/socks4.go b/flora/socks4.go index 99b6bba..6d1ed22 100644 --- a/flora/socks4.go +++ b/flora/socks4.go @@ -1,10 +1,10 @@ package flora import ( - "io" - "net" "encoding/binary" "fmt" + "io" + "net" ) /* @@ -32,7 +32,7 @@ byte | 0 | 1 | 2 | 3 | 4 | 5 | 6| 7 | */ // local socks server connect -func socks4Connect(conn net.Conn,first byte ) (addr string, hostType int, err error) { +func socks4Connect(conn net.Conn, first byte) (addr string, hostType int, err error) { const ( idVer = 0 idStatus = 1 @@ -53,16 +53,16 @@ func socks4Connect(conn net.Conn,first byte ) (addr string, hostType int, err er if n, err = io.ReadAtLeast(conn, buf[1:], id4aFixLen); err != nil { return } - n ++ + n++ // command only support connect if buf[idStatus] != cmdConnect { return } // get port - port := binary.BigEndian.Uint16(buf[idPort:idPort+idPortLen]) + port := binary.BigEndian.Uint16(buf[idPort : idPort+idPortLen]) // get ip - ip := net.IP(buf[idIP:idIP+idIPLen]) + ip := net.IP(buf[idIP : idIP+idIPLen]) hostType = typeIPv4 var host = ip.String() diff --git a/flora/socks5.go b/flora/socks5.go index 81dbe0d..b2ffaec 100644 --- a/flora/socks5.go +++ b/flora/socks5.go @@ -1,12 +1,12 @@ package flora import ( + "encoding/binary" + ss "github.com/shadowsocks/shadowsocks-go/shadowsocks" "io" + "log" "net" "strconv" - ss "github.com/shadowsocks/shadowsocks-go/shadowsocks" - "encoding/binary" - "log" ) /* @@ -46,7 +46,7 @@ byte |0 | 1 | 2 | 3 | 4 | .. | n-2 | n-1 | n | */ //local socks server auth -func handshake(conn net.Conn,first byte ) (err error) { +func handshake(conn net.Conn, first byte) (err error) { const ( idVer = 0 idNmethod = 1 @@ -64,7 +64,7 @@ func handshake(conn net.Conn,first byte ) (err error) { if n, err = io.ReadAtLeast(conn, buf[1:], idNmethod+1); err != nil { return } - n ++ + n++ //if buf[idVer] != socksVer5 { // return errVer //} @@ -145,13 +145,13 @@ func socks5Connect(conn net.Conn) (host string, hostType int, err error) { //raw := buf[idType:reqLen] switch hostType { case typeIPv4: - host = net.IP(buf[idIP0: idIP0+net.IPv4len]).String() + host = net.IP(buf[idIP0 : idIP0+net.IPv4len]).String() case typeIPv6: - host = net.IP(buf[idIP0: idIP0+net.IPv6len]).String() + host = net.IP(buf[idIP0 : idIP0+net.IPv6len]).String() case typeDm: - host = string(buf[idDm0: idDm0+buf[idDmLen]]) + host = string(buf[idDm0 : idDm0+buf[idDmLen]]) } - port := binary.BigEndian.Uint16(buf[reqLen-2: reqLen]) + port := binary.BigEndian.Uint16(buf[reqLen-2 : reqLen]) host = net.JoinHostPort(host, strconv.Itoa(int(port))) _, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43}) return diff --git a/geoip_test.go b/geoip_test.go index bd3d5b8..832e185 100644 --- a/geoip_test.go +++ b/geoip_test.go @@ -2,8 +2,8 @@ package main_test import ( "flora-kit/flora" - "testing" "os" + "testing" ) func TestGeoIP(t *testing.T) {