Skip to content

Commit

Permalink
Merge branch 'develop', fix crash when auth IPv6 client.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Jul 19, 2013
2 parents 40cdfbd + a2f0814 commit 7a28cf1
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 72 deletions.
4 changes: 2 additions & 2 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func initAuth() {
// Return err = nil if authentication succeed. nonce would be not empty if
// authentication is needed, and should be passed back on subsequent call.
func Authenticate(conn *clientConn, r *Request) (err error) {
clientIP, _ := splitHostPort(conn.RemoteAddr().String())
clientIP, _, _ := net.SplitHostPort(conn.RemoteAddr().String())
if auth.authed.has(clientIP) {
debug.Printf("%s has already authed\n", clientIP)
return
Expand Down Expand Up @@ -277,7 +277,7 @@ func checkProxyAuthorization(conn *clientConn, r *Request) error {

if au.port != 0 {
// check port
_, portStr := splitHostPort(conn.LocalAddr().String())
_, portStr, _ := net.SplitHostPort(conn.LocalAddr().String())
port, _ := strconv.Atoi(portStr)
if uint16(port) != au.port {
errl.Println("auth: user", user, "port not match")
Expand Down
34 changes: 16 additions & 18 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/cyfdecyf/bufio"
"io"
"net"
"os"
"path"
"reflect"
Expand Down Expand Up @@ -133,12 +134,9 @@ func parseDuration(val, msg string) (d time.Duration) {
return
}

func hasPort(val string) bool {
_, port := splitHostPort(val)
if port == "" {
return false
}
return true
func checkServerAddr(addr string) error {
_, _, err := net.SplitHostPort(addr)
return err
}

func isUserPasswdValid(val string) bool {
Expand All @@ -165,9 +163,9 @@ func (p configParser) ParseListen(val string) {
}
for _, s := range arr {
s = strings.TrimSpace(s)
host, port := splitHostPort(s)
if port == "" {
Fatalf("listen address %s has no port\n", s)
host, _, err := net.SplitHostPort(s)
if err != nil {
Fatal("listen address", err)
}
if host == "" || host == "0.0.0.0" {
if len(arr) > 1 {
Expand All @@ -187,9 +185,9 @@ func (p configParser) ParseAddrInPAC(val string) {
continue
}
s = strings.TrimSpace(s)
host, port := splitHostPort(s)
if port == "" {
Fatalf("proxy address in PAC %s has no port\n", s)
host, _, err := net.SplitHostPort(s)
if err != nil {
Fatal("proxy address in PAC", err)
}
if host == "0.0.0.0" {
Fatal("can't use 0.0.0.0 as proxy address in PAC")
Expand All @@ -201,8 +199,8 @@ func (p configParser) ParseAddrInPAC(val string) {
// error checking is done in check config

func (p configParser) ParseSocksParent(val string) {
if !hasPort(val) {
Fatal("parent socks server must have port specified")
if err := checkServerAddr(val); err != nil {
Fatal("parent socks server", err)
}
addParentProxy(newSocksParent(val))
}
Expand Down Expand Up @@ -230,8 +228,8 @@ var http struct {
}

func (p configParser) ParseHttpParent(val string) {
if !hasPort(val) {
Fatal("parent http server must have port specified")
if err := checkServerAddr(val); err != nil {
Fatal("parent http server", err)
}
config.hasHttpParent = true
http.parent = newHttpParent(val)
Expand Down Expand Up @@ -292,8 +290,8 @@ func (p configParser) ParseShadowSocks(val string) {
shadow.parent = nil
return
}
if !hasPort(val) {
Fatal("shadowsocks server must have port specified")
if err := checkServerAddr(val); err != nil {
Fatal("shadowsocks server", err)
}
shadow.parent = newShadowsocksParent(val)
addParentProxy(shadow.parent)
Expand Down
30 changes: 4 additions & 26 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,29 +205,6 @@ func (url *URL) String() string {
return url.HostPort + url.Path
}

// For port, return empty string if no port specified.
// This also works for IPv6 address.
func splitHostPort(s string) (host, port string) {
if len(s) == 0 {
return "", ""
}
// Common case should has no port, check the last char first
if !IsDigit(s[len(s)-1]) {
return s, ""
}
// Scan back, make sure we find ':'
for i := len(s) - 2; i >= 0; i-- {
c := s[i]
switch {
case c == ':':
return s[:i], s[i+1:]
case !IsDigit(c):
return s, ""
}
}
return s, ""
}

// net.ParseRequestURI will unescape encoded path, but the proxy doesn't need
// that. Assumes the input rawurl is valid. Even if rawurl is not valid, net.Dial
// will check the correctness of the host.
Expand Down Expand Up @@ -269,8 +246,9 @@ func ParseRequestURIBytes(rawurl []byte) (*URL, error) {
// Must add port in host so it can be used as key to find the correct
// server connection.
// e.g. google.com:80 and google.com:443 should use different connections.
host, port = splitHostPort(hostport)
if port == "" {
host, port, err := net.SplitHostPort(hostport)
if err != nil { // missing port
host = hostport
if len(scheme) == 4 {
hostport = net.JoinHostPort(host, "80")
port = "80"
Expand Down Expand Up @@ -593,7 +571,7 @@ func parseResponse(sv *serverConn, r *Request, rp *Response) (err error) {

proto := f[0]
if !bytes.Equal(proto[0:7], []byte("HTTP/1.")) {
errl.Printf("Invalid response status line: %s\n", string(f[0]))
errl.Printf("Invalid response status line: %s request %v\n", string(f[0]), r)
return errMalformResponse
}
if proto[7] == '1' {
Expand Down
20 changes: 0 additions & 20 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ import (
"time"
)

func TestSplitHostPort(t *testing.T) {
var testData = []struct {
hostPort string
hostNoPort string
port string
}{
{"google.com", "google.com", ""},
{"google.com:80", "google.com", "80"},
{"google.com80", "google.com80", ""},
{":7777", "", "7777"},
}

for _, td := range testData {
h, p := splitHostPort(td.hostPort)
if h != td.hostNoPort || p != td.port {
t.Errorf("%s returns %v:%v", td.hostPort, td.hostNoPort, td.port)
}
}
}

func TestParseRequestURI(t *testing.T) {
var testData = []struct {
rawurl string
Expand Down
8 changes: 7 additions & 1 deletion pac.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ func genPAC(c *clientConn) []byte {

proxyAddr := c.proxy.addrInPAC
if proxyAddr == "" {
host, _ := splitHostPort(c.LocalAddr().String())
host, _, err := net.SplitHostPort(c.LocalAddr().String())
// This is the only check to split host port on tcp addr's string
// representation in COW. Keep it so we will notice if there's any
// problem in the future.
if err != nil {
panic("split host port on local address error")
}
proxyAddr = net.JoinHostPort(host, c.proxy.port)
}

Expand Down
3 changes: 2 additions & 1 deletion parent_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ func (sp *shadowsocksParent) initCipher(passwd, method string) {
func (sp *shadowsocksParent) connect(url *URL) (conn, error) {
c, err := ss.Dial(url.HostPort, sp.server, sp.cipher.Copy())
if err != nil {
errl.Printf("can't create shadowsocks connection for: %s %v\n", url.HostPort, err)
errl.Printf("create shadowsocks connection to %s through server %s failed %v\n",
url.HostPort, sp.server, err)
return zeroConn, err
}
debug.Println("connected to:", url.HostPort, "via shadowsocks:", sp.server)
Expand Down
9 changes: 6 additions & 3 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ var (
)

func NewProxy(addr, addrInPAC string) *Proxy {
_, port := splitHostPort(addr)
_, port, err := net.SplitHostPort(addr)
if err != nil {
panic("proxy addr" + err.Error())
}
return &Proxy{addr: addr, port: port, addrInPAC: addrInPAC}
}

Expand All @@ -154,9 +157,9 @@ func (py *Proxy) Serve(done chan byte) {
fmt.Println("Server creation failed:", err)
return
}
host, port := splitHostPort(py.addr)
host, _, _ := net.SplitHostPort(py.addr)
if host == "" || host == "0.0.0.0" {
info.Printf("COW proxy address %s, PAC url http://<hostip>:%s/pac\n", py.addr, port)
info.Printf("COW proxy address %s, PAC url http://<hostip>:%s/pac\n", py.addr, py.port)
} else if py.addrInPAC == "" {
info.Printf("COW proxy address %s, PAC url http://%s/pac\n", py.addr, py.addr)
} else {
Expand Down
1 change: 0 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,6 @@ func trimLastDot(s string) string {
// host2Domain returns the domain of a host. It will recognize domains like
// google.com.hk. Returns empty string for simple host and internal IP.
func host2Domain(host string) (domain string) {
host, _ = splitHostPort(host)
isIP, isPrivate := hostIsIP(host)
if isPrivate {
return ""
Expand Down

0 comments on commit 7a28cf1

Please sign in to comment.