Skip to content

Commit

Permalink
code style related clear up
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanchu committed Jun 10, 2018
1 parent d394e71 commit 1801975
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
34 changes: 17 additions & 17 deletions cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func getRequest(conn *ss.Conn) (host string, err error) {
const logCntDelta = 100

var connCnt int
var nextLogConnCnt int = logCntDelta
var nextLogConnCnt = logCntDelta

func sanitizeAddr(addr net.Addr) string {
if sanitizeIps {
Expand Down Expand Up @@ -167,13 +167,13 @@ func handleConnection(conn *ss.Conn, port string) {
debug.Printf("piping %s<->%s", sanitizeAddr(conn.RemoteAddr()), host)
}
go func() {
ss.PipeThenClose(conn, remote, func(flow int) {
passwdManager.addFlow(port, flow)
ss.PipeThenClose(conn, remote, func(Traffic int) {
passwdManager.addTraffic(port, Traffic)
})
}()

ss.PipeThenClose(remote, conn, func(flow int) {
passwdManager.addFlow(port, flow)
ss.PipeThenClose(remote, conn, func(Traffic int) {
passwdManager.addTraffic(port, Traffic)
})

closed = true
Expand All @@ -194,13 +194,13 @@ type PasswdManager struct {
sync.Mutex
portListener map[string]*PortListener
udpListener map[string]*UDPListener
flowStats map[string]int64
trafficStats map[string]int64
}

func (pm *PasswdManager) add(port, password string, listener net.Listener) {
pm.Lock()
pm.portListener[port] = &PortListener{password, listener}
pm.flowStats[port] = 0
pm.trafficStats[port] = 0
pm.Unlock()
}

Expand Down Expand Up @@ -239,24 +239,24 @@ func (pm *PasswdManager) del(port string) {
pl.listener.Close()
pm.Lock()
delete(pm.portListener, port)
delete(pm.flowStats, port)
delete(pm.trafficStats, port)
if udp {
delete(pm.udpListener, port)
}
pm.Unlock()
}

func (pm *PasswdManager) addFlow(port string, n int) {
func (pm *PasswdManager) addTraffic(port string, n int) {
pm.Lock()
pm.flowStats[port] = pm.flowStats[port] + int64(n)
pm.trafficStats[port] = pm.trafficStats[port] + int64(n)
pm.Unlock()
return
}

func (pm *PasswdManager) getFlowStats() map[string]int64 {
func (pm *PasswdManager) getTrafficStats() map[string]int64 {
pm.Lock()
copy := make(map[string]int64)
for k, v := range pm.flowStats {
for k, v := range pm.trafficStats {
copy[k] = v
}
pm.Unlock()
Expand Down Expand Up @@ -299,7 +299,7 @@ func (pm *PasswdManager) updatePortPasswd(port, password string) {
var passwdManager = PasswdManager{
portListener: map[string]*PortListener{},
udpListener: map[string]*UDPListener{},
flowStats: map[string]int64{},
trafficStats: map[string]int64{},
}

func updatePasswd() {
Expand Down Expand Up @@ -394,8 +394,8 @@ func runUDP(port, password string) {
}
SecurePacketConn := ss.NewSecurePacketConn(conn, cipher.Copy())
for {
if err := ss.ReadAndHandleUDPReq(SecurePacketConn, func(flow int) {
passwdManager.addFlow(port, flow)
if err := ss.ReadAndHandleUDPReq(SecurePacketConn, func(traffic int) {
passwdManager.addTraffic(port, traffic)
}); err != nil {
debug.Printf("udp read error: %v\n", err)
return
Expand Down Expand Up @@ -601,10 +601,10 @@ func handlePing() []byte {
return []byte("pong")
}

// reportStat get the stat:flowStat and return avery 10 sec as for the protocol
// reportStat get the stat:trafficStat and return avery 10 sec as for the protocol
// https://github.com/shadowsocks/shadowsocks/wiki/Manage-Multiple-Users
func reportStat() []byte {
stats := passwdManager.getFlowStats()
stats := passwdManager.getTrafficStats()
var buf bytes.Buffer
buf.WriteString("stat: ")
ret, _ := json.Marshal(stats)
Expand Down
6 changes: 3 additions & 3 deletions shadowsocks/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

const text = "Don't tell me the moon is shining; show me the glint of light on broken glass."

func testCiphter(t *testing.T, c *Cipher, msg string) {
func testCipher(t *testing.T, c *Cipher, msg string) {
n := len(text)
cipherBuf := make([]byte, n)
originTxt := make([]byte, n)
Expand Down Expand Up @@ -51,7 +51,7 @@ func testBlockCipher(t *testing.T, method string) {
if err = cipher.initDecrypt(iv); err != nil {
t.Error(method, "initDecrypt:", err)
}
testCiphter(t, cipher, method)
testCipher(t, cipher, method)

iv, err = cipherCopy.initEncrypt()
if err != nil {
Expand All @@ -60,7 +60,7 @@ func testBlockCipher(t *testing.T, method string) {
if err = cipherCopy.initDecrypt(iv); err != nil {
t.Error(method, "copy initDecrypt:", err)
}
testCiphter(t, cipherCopy, method+" copy")
testCipher(t, cipherCopy, method+" copy")
}

func TestAES128CFB(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions shadowsocks/mergesort.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ func merge(left, right []uint64, comparison func (uint64, uint64) int64) []uint6
r++
}
}
for (l < len(left)) {
for l < len(left) {
result[l + r] = left[l]
l++
}
for (r < len(right)) {
for r < len(right) {
result[l + r] = right[r]
r++
}
Expand All @@ -27,6 +27,6 @@ func Sort(arr []uint64, comparison func (uint64, uint64) int64) []uint64 {
if len(arr) < 2 {
return arr
}
var middle uint64 = uint64(len(arr)/2)
var middle = uint64(len(arr)/2)
return merge(Sort(arr[0:middle], comparison), Sort(arr[middle:], comparison), comparison)
}
6 changes: 3 additions & 3 deletions shadowsocks/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ func SetReadTimeout(c net.Conn) {
}

// PipeThenClose copies data from src to dst, closes dst when done.
func PipeThenClose(src, dst net.Conn, addFlow func(int)) {
func PipeThenClose(src, dst net.Conn, addTraffic func(int)) {
defer dst.Close()
buf := leakyBuf.Get()
defer leakyBuf.Put(buf)
for {
SetReadTimeout(src)
n, err := src.Read(buf)
if addFlow != nil {
addFlow(n)
if addTraffic != nil {
addTraffic(n)
}
// read may return EOF with n > 0
// should always process n > 0 bytes before handling error
Expand Down
2 changes: 1 addition & 1 deletion shadowsocks/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ProxyAddr struct {
address string
}

var ErrNilCipher = errors.New("cipher can't be nil.")
var ErrNilCipher = errors.New("cipher can't be nil")

func NewDialer(server string, cipher *Cipher) (dialer *Dialer, err error) {
// Currently shadowsocks-go do not support UDP
Expand Down
18 changes: 10 additions & 8 deletions shadowsocks/udprelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ func (table *natTable) Get(index string) (c net.PacketConn, ok bool, err error)
return
}

//noinspection GoRedundantParens
type requestHeaderList struct {
sync.Mutex
List map[string]([]byte)
}

//noinspection GoRedundantParens
func newReqList() *requestHeaderList {
ret := &requestHeaderList{List: map[string]([]byte){}}
go func() {
Expand Down Expand Up @@ -130,7 +132,7 @@ func parseHeaderFromAddr(addr net.Addr) ([]byte, int) {
return buf[:1+iplen+2], 1 + iplen + 2
}

func Pipeloop(write net.PacketConn, writeAddr net.Addr, readClose net.PacketConn, addFlow func(int)) {
func Pipeloop(write net.PacketConn, writeAddr net.Addr, readClose net.PacketConn, addTraffic func(int)) {
buf := leakyBuf.Get()
defer leakyBuf.Put(buf)
defer readClose.Close()
Expand All @@ -151,16 +153,16 @@ func Pipeloop(write net.PacketConn, writeAddr net.Addr, readClose net.PacketConn
// need improvement here
if req, ok := reqList.Get(raddr.String()); ok {
n, _ := write.WriteTo(append(req, buf[:n]...), writeAddr)
addFlow(n)
addTraffic(n)
} else {
header, hlen := parseHeaderFromAddr(raddr)
n, _ := write.WriteTo(append(header[:hlen], buf[:n]...), writeAddr)
addFlow(n)
addTraffic(n)
}
}
}

func handleUDPConnection(handle *SecurePacketConn, n int, src net.Addr, receive []byte, addFlow func(int)) {
func handleUDPConnection(handle *SecurePacketConn, n int, src net.Addr, receive []byte, addTraffic func(int)) {
var dstIP net.IP
var reqLen int
addrType := receive[idType]
Expand Down Expand Up @@ -217,7 +219,7 @@ func handleUDPConnection(handle *SecurePacketConn, n int, src net.Addr, receive
if !exist {
Debug.Printf("[udp]new client %s->%s via %s\n", src, dst, remote.LocalAddr())
go func() {
Pipeloop(handle, src, remote, addFlow)
Pipeloop(handle, src, remote, addTraffic)
natlist.Delete(src.String())
}()
} else {
Expand All @@ -228,7 +230,7 @@ func handleUDPConnection(handle *SecurePacketConn, n int, src net.Addr, receive
}
remote.SetDeadline(time.Now().Add(udpTimeout))
n, err = remote.WriteTo(receive[reqLen:n], dst)
addFlow(n)
addTraffic(n)
if err != nil {
if ne, ok := err.(*net.OpError); ok && (ne.Err == syscall.EMFILE || ne.Err == syscall.ENFILE) {
// log too many open file error
Expand All @@ -245,12 +247,12 @@ func handleUDPConnection(handle *SecurePacketConn, n int, src net.Addr, receive
return
}

func ReadAndHandleUDPReq(c *SecurePacketConn, addFlow func(int)) error {
func ReadAndHandleUDPReq(c *SecurePacketConn, addTraffic func(int)) error {
buf := leakyBuf.Get()
n, src, err := c.ReadFrom(buf[0:])
if err != nil {
return err
}
go handleUDPConnection(c, n, src, buf, addFlow)
go handleUDPConnection(c, n, src, buf, addTraffic)
return nil
}

0 comments on commit 1801975

Please sign in to comment.