Skip to content

Commit

Permalink
Print debug message using logging facility.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Dec 12, 2012
1 parent a37ff3f commit f257c21
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
25 changes: 14 additions & 11 deletions cmd/shadowsocks-local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package main

import (
"fmt"
"github.com/shadowsocks/shadowsocks-go/shadowsocks"
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
"log"
"net"
)

var config *shadowsocks.Config
var encTbl *shadowsocks.EncryptTable
var config *ss.Config
var encTbl *ss.EncryptTable

var debug ss.DebugLog

func handleConnection(conn net.Conn, server string) {
log.Printf("socks connect from %s\n", conn.RemoteAddr().String())
debug.Printf("socks connect from %s\n", conn.RemoteAddr().String())
b := make([]byte, 262)
var err error = nil
var hasError = false
Expand Down Expand Up @@ -48,19 +50,19 @@ func handleConnection(conn net.Conn, server string) {
log.Println("unsurpported addr type")
break
}
log.Println("connecting ", addr)
debug.Println("connecting ", addr)
conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})

remote, err := shadowsocks.DialWithRawAddr(addrToSend, server, encTbl)
remote, err := ss.DialWithRawAddr(addrToSend, server, encTbl)
if err != nil {
hasError = true
break
}
c := make(chan int, 2)
go shadowsocks.Pipe(conn, remote, c)
go shadowsocks.Pipe(remote, conn, c)
go ss.Pipe(conn, remote, c)
go ss.Pipe(remote, conn, c)
<-c // close the other connection whenever one connection is closed
log.Println("closing")
debug.Println("closing")
err = conn.Close()
err1 := remote.Close()
if err == nil {
Expand Down Expand Up @@ -98,7 +100,8 @@ func run(port int, server string) {
}

func main() {
config = shadowsocks.ParseConfig("config.json")
encTbl = shadowsocks.GetTable(config.Password)
config = ss.ParseConfig("config.json")
debug = ss.Debug
encTbl = ss.GetTable(config.Password)
run(config.LocalPort, fmt.Sprintf("%s:%d", config.Server, config.ServerPort))
}
32 changes: 17 additions & 15 deletions cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package main
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/shadowsocks/shadowsocks-go/shadowsocks"
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
"log"
"net"
"strconv"
)

func handleConnection(conn *shadowsocks.Conn) {
log.Printf("socks connect from %s\n", conn.RemoteAddr().String())
var debug ss.DebugLog

func handleConnection(conn *ss.Conn) {
debug.Printf("socks connect from %s\n", conn.RemoteAddr().String())
var err error = nil
var hasError = false
for {
Expand Down Expand Up @@ -56,12 +57,12 @@ func handleConnection(conn *shadowsocks.Conn) {
binary.Read(sb, binary.BigEndian, &port)
} else {
hasError = true
log.Println("unsurpported addr type")
debug.Println("unsurpported addr type")
break
}
log.Println("connecting ", addr)
debug.Println("connecting ", addr)
var remote net.Conn
remote, err = net.Dial("tcp", fmt.Sprintf("%s:%d", addr, port))
remote, err = net.Dial("tcp", addr+":"+strconv.Itoa(int(port)))
if err != nil {
hasError = true
break
Expand All @@ -71,10 +72,10 @@ func handleConnection(conn *shadowsocks.Conn) {
break
}
c := make(chan int, 2)
go shadowsocks.Pipe(conn, remote, c)
go shadowsocks.Pipe(remote, conn, c)
go ss.Pipe(conn, remote, c)
go ss.Pipe(remote, conn, c)
<-c // close the other connection whenever one connection is closed
log.Println("closing")
debug.Println("closing")
err = conn.Close()
err1 := remote.Close()
if err == nil {
Expand All @@ -84,11 +85,11 @@ func handleConnection(conn *shadowsocks.Conn) {
}
if err != nil || hasError {
if err != nil {
log.Println("error ", err)
debug.Println("error", err)
}
err = conn.Close()
if err != nil {
log.Println("close:", err)
debug.Println("close:", err)
}
return
}
Expand All @@ -100,20 +101,21 @@ func run(port, password string) {
if err != nil {
log.Fatal(err)
}
encTbl := shadowsocks.GetTable(password)
encTbl := ss.GetTable(password)
log.Printf("starting server at port %v ...\n", port)
for {
conn, err := ln.Accept()
if err != nil {
log.Println("accept:", err)
continue
}
go handleConnection(shadowsocks.NewConn(conn, encTbl))
go handleConnection(ss.NewConn(conn, encTbl))
}
}

func main() {
config := shadowsocks.ParseConfig("config.json")
config := ss.ParseConfig("config.json")
debug = ss.Debug
if len(config.PortPassword) == 0 {
run(strconv.Itoa(config.ServerPort), config.Password)
} else {
Expand Down
5 changes: 2 additions & 3 deletions shadowsocks/pipe.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package shadowsocks

import (
"log"
"net"
)

Expand All @@ -18,15 +17,15 @@ func Pipe(src, dst net.Conn, end chan int) {
// should always process num > 0 bytes before handling error
if num > 0 {
if _, err = dst.Write(buf[0:num]); err != nil {
log.Println("write:", err)
Debug.Println("write:", err)
break
}
}
if num == 0 { // num == 0 should associate with EOF
break
}
if err != nil {
log.Println("read:", err)
Debug.Println("read:", err)
break
}
}
Expand Down

0 comments on commit f257c21

Please sign in to comment.