Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
Fix some bug for IPv6
  • Loading branch information
linimbus committed Sep 13, 2024
1 parent 1e1add6 commit b867ff0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
9 changes: 5 additions & 4 deletions engin/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
Expand Down Expand Up @@ -111,7 +110,7 @@ func DebugReqeust(r *http.Request) {
for key, value := range r.Header {
headers += fmt.Sprintf("[%s:%s]", key, value)
}
logs.Info("%s %s %s %s %s %s", r.RemoteAddr, r.Host, r.URL.Scheme, r.Method, r.URL.String(), headers)
logs.Info("RemoteAddr:%s Host:%s Scheme:%s Method:%s URL:%s Header:%s", r.RemoteAddr, r.Host, r.URL.Scheme, r.Method, r.URL.String(), headers)
}

func (acc *HttpAccess) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -169,7 +168,9 @@ func (acc *HttpAccess) ServeHTTP(w http.ResponseWriter, r *http.Request) {

size, err := io.Copy(w, rsp.Body)
if size == 0 && err != nil {
logs.Warn("io copy fail", err.Error())
if err != io.EOF {
logs.Info("io copy fail", err.Error())
}
} else {
StatUpdate(0, size)
}
Expand Down Expand Up @@ -256,7 +257,7 @@ func (acc *HttpAccess) HttpsRoundTripper(w http.ResponseWriter, r *http.Request)

func (acc *HttpAccess) HttpRoundTripper(r *http.Request) (*http.Response, error) {
if r.Body != nil {
r.Body = ioutil.NopCloser(r.Body)
r.Body = io.NopCloser(r.Body)
}
return acc.HttpForward(Address(r.URL), r)
}
Expand Down
5 changes: 1 addition & 4 deletions engin/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,5 @@ func AuthCache(r *http.Request) bool {
defer authctrl.RUnlock()

_, ok := authctrl.Cache[address]
if ok {
return true
}
return false
return ok
}
61 changes: 54 additions & 7 deletions engin/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var (
)

func init() {
rand.Seed(time.Now().UnixNano())
uuid = fmt.Sprintf("%08X-%016X-%08X", rand.Int31(), rand.Int63(), rand.Int31())
}

Expand All @@ -41,13 +40,59 @@ func IsConnect(address string, timeout int) bool {
return true
}

/* covert HOST to ip address with port
[2409:8a28:c4d:a181::42d]:8070 -> [2409:8a28:c4d:a181::42d]:8070
[2409:8a28:c4d:a181::42d]:8070 -> [2409:8a28:c4d:a181::42d]:8070
2409:8a28:c4d:a181::42d:8070 -> [2409:8a28:c4d:a181::42d]:8070
2409:8a28:c4d:a181::42d:8070 -> [2409:8a28:c4d:a181::42d]:8070
2409:8a28:c4d:a181::42d -> [2409:8a28:c4d:a181::42d]:80
2409:8a28:c4d:a181::42d -> [2409:8a28:c4d:a181::42d]:443
192.168.3.1:111 -> 192.168.3.1:111
192.168.3.1:111 -> 192.168.3.1:111
192.168.1.1 -> 192.168.1.1:80
192.168.1.1 -> 192.168.1.1:443
demo.abc.a:111 -> demo.abc.a:111
demo.abc.a:111 -> demo.abc.a:111
demo.abc -> demo.abc:80
demo.abc -> demo.abc:443
*/

func Address(u *url.URL) string {
host := u.Host
if strings.Index(host, ":") == -1 {
if strings.ToLower(u.Scheme) == "https" {
host += "443"
} else {
host += "80"

var defaut_port string
if strings.ToLower(u.Scheme) == "https" {
defaut_port = "443"
} else {
defaut_port = "80"
}

count := strings.Count(host, ":")
if count == 0 {
return host + ":" + defaut_port
}

if count > 1 {
index := strings.LastIndex(host, ":")
addr := host[:index]
port := host[index+1:]

ip := net.ParseIP(addr)
if ip != nil {
if len(ip.To4()) == net.IPv4len {
return ip.String() + ":" + port
} else {
return "[" + ip.String() + "]:" + port
}
}

ip = net.ParseIP(host)
if ip != nil {
if len(ip.To4()) == net.IPv4len {
return ip.String() + ":" + defaut_port
} else {
return "[" + ip.String() + "]:" + defaut_port
}
}
}
return host
Expand All @@ -74,7 +119,9 @@ func iocopy(c *sync.WaitGroup, in net.Conn, out net.Conn) {

size, err := io.Copy(in, out)
if size == 0 && err != nil {
logs.Warn("io copy fail", err.Error())
if err != io.EOF {
logs.Info("io copy fail", err.Error())
}
} else {
StatUpdate(0, size)
}
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ func init() {
}

func VersionGet() string {
return "v1.7.1"
return "v1.7.2"
}

0 comments on commit b867ff0

Please sign in to comment.