Skip to content

Commit

Permalink
Use shadowsocks.Conn in server and client.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Dec 7, 2012
1 parent 181005e commit 4e9cad4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 35 deletions.
24 changes: 9 additions & 15 deletions src/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"shadowsocks"
)

func handleConnection(conn net.Conn, encryptTable, decryptTable []byte, server string) {
func handleConnection(conn net.Conn, server string) {
log.Printf("socks connect from %s\n", conn.RemoteAddr().String())
b := make([]byte, 262)
var err error = nil
Expand Down Expand Up @@ -47,20 +47,15 @@ func handleConnection(conn net.Conn, encryptTable, decryptTable []byte, server s
}
log.Println("connecting ", addr)
conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})
var remote net.Conn
remote, err = net.Dial("tcp", server)
if err != nil {
hasError = true
break
}
_, err = remote.Write(shadowsocks.Encrypt(encryptTable, addrToSend))

remote, err := shadowsocks.DialWithAddrBuf(addrToSend, server)
if err != nil {
hasError = true
break
}
c := make(chan int, 2)
go shadowsocks.Pipe(conn, remote, encryptTable, c)
go shadowsocks.Pipe(remote, conn, decryptTable, c)
go shadowsocks.Pipe(conn, remote, c)
go shadowsocks.Pipe(remote, conn, c)
<-c // close the other connection whenever one connection is closed
log.Println("closing")
err = conn.Close()
Expand All @@ -83,7 +78,7 @@ func handleConnection(conn net.Conn, encryptTable, decryptTable []byte, server s

}

func run(encryptTable, decryptTable []byte, port int, server string) {
func run(port int, server string) {
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatal(err)
Expand All @@ -95,13 +90,12 @@ func run(encryptTable, decryptTable []byte, port int, server string) {
log.Println("accept:", err)
continue
}
go handleConnection(net.Conn(conn), encryptTable, decryptTable, server)
go handleConnection(conn, server)
}
}

func main() {
config := shadowsocks.ParseConfig()
encyrptTable, decryptTable := shadowsocks.GetTable(config.Password)
run(encyrptTable, decryptTable, config.LocalPort, fmt.Sprintf("%s:%d", config.Server, config.ServerPort))

shadowsocks.InitTable(config.Password)
run(config.LocalPort, fmt.Sprintf("%s:%d", config.Server, config.ServerPort))
}
28 changes: 12 additions & 16 deletions src/server/server.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package main

import (
"shadowsocks"
"net"
"bytes"
"log"
"encoding/binary"
"fmt"
"log"
"net"
"shadowsocks"
)

func handleConnection(conn net.Conn, encryptTable, decryptTable []byte) {
func handleConnection(conn shadowsocks.Conn) {
log.Printf("socks connect from %s\n", conn.RemoteAddr().String())
var err error = nil
var hasError = false
Expand All @@ -21,7 +21,6 @@ func handleConnection(conn net.Conn, encryptTable, decryptTable []byte) {
hasError = true
break
}
buf = shadowsocks.Encrypt(decryptTable, buf)
addrType := buf[0]
var addr string
var port int16
Expand All @@ -32,7 +31,6 @@ func handleConnection(conn net.Conn, encryptTable, decryptTable []byte) {
hasError = true
break
}
buf = shadowsocks.Encrypt(decryptTable, buf)
var addrIp net.IP = make(net.IP, 4)
copy(addrIp, buf[0:4])
addr = addrIp.String()
Expand All @@ -44,18 +42,16 @@ func handleConnection(conn net.Conn, encryptTable, decryptTable []byte) {
hasError = true
break
}
buf = shadowsocks.Encrypt(decryptTable, buf)
addrLen := buf[0]
buf = make([]byte, addrLen + 2)
buf = make([]byte, addrLen+2)
_, err = conn.Read(buf)
if err != nil {
hasError = true
break
}
buf = shadowsocks.Encrypt(decryptTable, buf)
sb := bytes.NewBuffer(buf[0:addrLen])
addr = sb.String()
sb = bytes.NewBuffer(buf[addrLen:addrLen + 2])
sb = bytes.NewBuffer(buf[addrLen : addrLen+2])
binary.Read(sb, binary.BigEndian, &port)
} else {
hasError = true
Expand All @@ -74,8 +70,8 @@ func handleConnection(conn net.Conn, encryptTable, decryptTable []byte) {
break
}
c := make(chan int, 2)
go shadowsocks.Pipe(conn, remote, decryptTable, c)
go shadowsocks.Pipe(remote, conn, encryptTable, c)
go shadowsocks.Pipe(conn, remote, c)
go shadowsocks.Pipe(remote, conn, c)
<-c // close the other connection whenever one connection is closed
log.Println("closing")
err = conn.Close()
Expand All @@ -98,7 +94,7 @@ func handleConnection(conn net.Conn, encryptTable, decryptTable []byte) {

}

func run(encryptTable, decryptTable []byte, port int) {
func run(port int) {
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatal(err)
Expand All @@ -110,12 +106,12 @@ func run(encryptTable, decryptTable []byte, port int) {
log.Println("accept:", err)
continue
}
go handleConnection(net.Conn(conn), encryptTable, decryptTable)
go handleConnection(shadowsocks.Conn{conn})
}
}

func main() {
config := shadowsocks.ParseConfig()
encyrptTable, decryptTable := shadowsocks.GetTable(config.Password)
run(encyrptTable, decryptTable, config.ServerPort)
shadowsocks.InitTable(config.Password)
run(config.ServerPort)
}
7 changes: 3 additions & 4 deletions src/shadowsocks/pipe.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package shadowsocks

import (
"net"
"log"
"net"
)

func Pipe(src net.Conn, dst net.Conn, table []byte, end chan int) {
func Pipe(src net.Conn, dst net.Conn, end chan int) {
buf := make([]byte, 4096)
for {
num, err := src.Read(buf)
if err == nil {
_, err := dst.Write(Encrypt(table, buf[0:num]))
_, err := dst.Write(buf[0:num])
if err != nil {
log.Println("write:", err)
end <- 1
Expand All @@ -27,4 +27,3 @@ func Pipe(src net.Conn, dst net.Conn, table []byte, end chan int) {
}
}
}

0 comments on commit 4e9cad4

Please sign in to comment.