Skip to content

Commit

Permalink
swap upload and download
Browse files Browse the repository at this point in the history
  • Loading branch information
p4gefau1t committed Mar 21, 2020
1 parent 0629b1b commit e1f8655
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 27 deletions.
12 changes: 6 additions & 6 deletions protocol/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ type MuxConnSession struct {
conn io.ReadWriteCloser
passwordHash string
meter stat.TrafficMeter
download int
upload int
recv int
sent int
}

func (m *MuxConnSession) Read(p []byte) (int, error) {
n, err := m.bufReadWriter.Read(p)
m.download += n
m.recv += n
return n, err
}

func (m *MuxConnSession) Write(p []byte) (int, error) {
n, err := m.bufReadWriter.Write(p)
m.bufReadWriter.Flush()
m.upload += n
m.sent += n
return n, err
}

func (m *MuxConnSession) Close() error {
m.meter.Count(m.passwordHash, m.upload, m.download)
logger.Info("mux conn to", m.request, "closed", "up:", common.HumanFriendlyTraffic(m.upload), "down:", common.HumanFriendlyTraffic(m.download))
m.meter.Count(m.passwordHash, m.sent, m.recv)
logger.Info("mux conn to", m.request, "closed", "sent:", common.HumanFriendlyTraffic(m.sent), "recv:", common.HumanFriendlyTraffic(m.recv))
return m.conn.Close()
}

Expand Down
12 changes: 6 additions & 6 deletions protocol/trojan/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ type TrojanInboundConnSession struct {
conn net.Conn
auth stat.Authenticator
meter stat.TrafficMeter
uploaded int
downloaded int
sent int
recv int
passwordHash string
}

func (i *TrojanInboundConnSession) Write(p []byte) (int, error) {
n, err := i.bufReadWriter.Write(p)
i.bufReadWriter.Flush()
i.uploaded += n
i.sent += n
return n, err
}

func (i *TrojanInboundConnSession) Read(p []byte) (int, error) {
n, err := i.bufReadWriter.Read(p)
i.downloaded += n
i.recv += n
return n, err
}

func (i *TrojanInboundConnSession) Close() error {
logger.Info("user", i.passwordHash, "conn to", i.request, "closed", "up:", common.HumanFriendlyTraffic(i.uploaded), "down:", common.HumanFriendlyTraffic(i.downloaded))
i.meter.Count(i.passwordHash, i.uploaded, i.downloaded)
logger.Info("user", i.passwordHash, "conn to", i.request, "closed", "sent:", common.HumanFriendlyTraffic(i.sent), "recv:", common.HumanFriendlyTraffic(i.recv))
i.meter.Count(i.passwordHash, i.sent, i.recv)
return i.conn.Close()
}

Expand Down
10 changes: 5 additions & 5 deletions protocol/trojan/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ type TrojanOutboundConnSession struct {
conn io.ReadWriteCloser
bufReadWriter *bufio.ReadWriter
request *protocol.Request
uploaded int
downloaded int
sent int
recv int
}

func (o *TrojanOutboundConnSession) Write(p []byte) (int, error) {
n, err := o.bufReadWriter.Write(p)
o.bufReadWriter.Flush()
o.uploaded += n
o.sent += n
return n, err
}

func (o *TrojanOutboundConnSession) Read(p []byte) (int, error) {
n, err := o.bufReadWriter.Read(p)
o.downloaded += n
o.recv += n
return n, err
}

func (o *TrojanOutboundConnSession) Close() error {
logger.Info("conn to", o.request, "closed", "up:", common.HumanFriendlyTraffic(o.uploaded), "down:", common.HumanFriendlyTraffic(o.downloaded))
logger.Info("conn to", o.request, "closed", "sent:", common.HumanFriendlyTraffic(o.sent), "recv:", common.HumanFriendlyTraffic(o.recv))
return o.conn.Close()
}

Expand Down
19 changes: 10 additions & 9 deletions stat/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

type trafficInfo struct {
passwordHash string
download int
upload int
recv int
sent int
}

type DBTrafficMeter struct {
Expand All @@ -27,11 +27,11 @@ const (
statsUpdateDuration = time.Second * 5
)

func (c *DBTrafficMeter) Count(passwordHash string, upload int, download int) {
func (c *DBTrafficMeter) Count(passwordHash string, sent int, recv int) {
c.trafficChan <- &trafficInfo{
passwordHash: passwordHash,
upload: upload,
download: download,
sent: sent,
recv: recv,
}
}

Expand All @@ -54,8 +54,8 @@ func (c *DBTrafficMeter) dbDaemon() {
}
statBuffer[u.passwordHash] = t
}
t.upload += u.upload
t.download += u.download
t.sent += u.sent
t.recv += u.recv
case <-time.After(statsUpdateDuration):
break
case <-c.ctx.Done():
Expand All @@ -74,13 +74,14 @@ func (c *DBTrafficMeter) dbDaemon() {
continue
}
for _, traffic := range statBuffer {
//swap upload and download for users
s, err := tx.Prepare("UPDATE users SET upload=upload+? WHERE password=?;")
common.Must(err)
_, err = s.Exec(traffic.upload, traffic.passwordHash)
_, err = s.Exec(traffic.recv, traffic.passwordHash)

s, err = tx.Prepare("UPDATE users SET download=download+? WHERE password=?;")
common.Must(err)
_, err = s.Exec(traffic.download, traffic.passwordHash)
_, err = s.Exec(traffic.sent, traffic.passwordHash)

if err != nil {
logger.Error(common.NewError("failed to update data to tx").Base(err))
Expand Down
2 changes: 1 addition & 1 deletion stat/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var logger = log.New(os.Stdout).WithColor()

type TrafficMeter interface {
Count(passwordHash string, upload int, download int)
Count(passwordHash string, sent int, recv int)
io.Closer
}

Expand Down

0 comments on commit e1f8655

Please sign in to comment.