Skip to content

Commit

Permalink
implement server
Browse files Browse the repository at this point in the history
  • Loading branch information
clowwindy committed Nov 1, 2012
1 parent 808c77b commit 15d6ae3
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/local/local.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func handleConnection(conn net.Conn, encryptTable, decryptTable []byte, server s
break
}
log.Println("connecting ", addr)
conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
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 {
Expand Down
122 changes: 122 additions & 0 deletions src/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package main

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

func handleConnection(conn net.Conn, encryptTable, decryptTable []byte) {
log.Printf("socks connect from %s\n", conn.RemoteAddr().String())
var err error = nil
var hasError = false
for {
var _ int
buf := make([]byte, 1)
_, err = conn.Read(buf)
if err != nil {
hasError = true
break
}
buf = shadowsocks.Encrypt(decryptTable, buf)
addrType := buf[0]
var addr string
var port int16
if addrType == 1 {
buf = make([]byte, 6)
_, err = conn.Read(buf)
if err != nil {
hasError = true
break
}
buf = shadowsocks.Encrypt(decryptTable, buf)
var addrIp net.IP = make(net.IP, 4)
copy(addrIp, buf[0:4])
addr = addrIp.String()
sb := bytes.NewBuffer(buf[4:6])
binary.Read(sb, binary.BigEndian, &port)
} else if addrType == 3 {
_, err = conn.Read(buf)
if err != nil {
hasError = true
break
}
buf = shadowsocks.Encrypt(decryptTable, buf)
addrLen := buf[0]
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])
binary.Read(sb, binary.BigEndian, &port)
} else {
hasError = true
log.Println("unsurpported addr type")
break
}
log.Println("connecting ", addr)
var remote net.Conn
remote, err = net.Dial("tcp", fmt.Sprintf("%s:%d", addr, port))
if err != nil {
hasError = true
break
}
if err != nil {
hasError = true
break
}
c := make(chan int, 2)
go shadowsocks.Pipe(conn, remote, decryptTable, c)
go shadowsocks.Pipe(remote, conn, encryptTable, c)
<-c // close the other connection whenever one connection is closed
log.Println("closing")
err = conn.Close()
err1 := remote.Close()
if err == nil {
err = err1
}
break
}
if err != nil || hasError {
if err != nil {
log.Println("error ", err)
}
err = conn.Close()
if err != nil {
log.Println("close:", err)
}
return
}

}

func run(encryptTable, decryptTable []byte, port int) {
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Println(err)
return
}
log.Printf("starting server at port %d ...\n", port)
for {
conn, err := ln.Accept()
if err != nil {
log.Println("accept:", err)
continue
}
go handleConnection(net.Conn(conn), encryptTable, decryptTable)
}
}

func main() {
encyrptTable, decryptTable := shadowsocks.GetTable("foobar!")
run(encyrptTable, decryptTable, 8388)

}
Empty file modified src/shadowsocks/encrypt.go
100755 → 100644
Empty file.
Empty file modified src/shadowsocks/mergesort.go
100755 → 100644
Empty file.
Empty file modified src/shadowsocks/pipe.go
100755 → 100644
Empty file.
Empty file modified src/test/test.go
100755 → 100644
Empty file.

0 comments on commit 15d6ae3

Please sign in to comment.