Skip to content

Commit

Permalink
优化流量显示信息,当获取流量信息时,自动转换成KB,MB,GB
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-zhangtao committed Oct 13, 2016
1 parent 8d74dad commit 8d725d3
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 58 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Forked from shadowsocks/shadowsocks-go
| /user/stop/{ports} | Stop specify proxy |
| /user/restart | Restart specify proxy |
| /user/expiry | Modify specify proxy expiry date |
| /user/rate | Get all the proxy rate |
| /user/info | Get all the proxy info |

* Misc API
Expand Down
3 changes: 2 additions & 1 deletion cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ func main() {
r.HandleFunc("/user/stop/{ports}", handler.DeleteListenHandler).Methods(http.MethodDelete)
r.HandleFunc("/user/restart", handler.RestartListenHandler).Methods(http.MethodPut)
r.HandleFunc("/user/expiry", handler.SetExpiryHandler).Methods(http.MethodPut)
r.HandleFunc("/user/info", handler.GetRateHandler).Methods(http.MethodGet)
r.HandleFunc("/user/info", handler.GetInfoHandler).Methods(http.MethodGet)
r.HandleFunc("/user/rate", handler.GetRateHandler).Methods(http.MethodGet)

r.HandleFunc("/version", handler.GetVersion).Methods(http.MethodGet)

Expand Down
22 changes: 20 additions & 2 deletions shadowsocks/handler/rateHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,29 @@ import (
"github.com/andy-zhangtao/Sandstorm"
)

// GetRateHandler 获取当前所有的端口流量
func GetRateHandler(w http.ResponseWriter, r *http.Request) {
// GetInfoHandler 获取当前所有的端口信息
func GetInfoHandler(w http.ResponseWriter, r *http.Request) {
keys := ss.GetRate()

content, _ := json.Marshal(keys)

Sandstorm.HTTPSuccess(w, string(content))
}

// GetRateHandler 获取当前所有端口流量数据
func GetRateHandler(w http.ResponseWriter, r *http.Request) {
keys := ss.GetRate()
rate := make([]*ss.Rate, len(keys))

for i, k := range keys {
r := &ss.Rate{
Port: k.Port,
Rate: ss.ConvertRate(k.Rate),
}
rate[i] = r
}

content, _ := json.Marshal(rate)

Sandstorm.HTTPSuccess(w, string(content))
}
2 changes: 1 addition & 1 deletion shadowsocks/handler/versionHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// GetVersion 获取当前版本信息
func GetVersion(w http.ResponseWriter, r *http.Request) {

dv := "Dev Version: " + "r5M d93828f"
dv := "Dev Version: " + "r6M 8d74dad"
rv := " Release Version: 0.1"
Sandstorm.HTTPSuccess(w, dv+rv)
}
6 changes: 1 addition & 5 deletions shadowsocks/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
var listenMap map[string]Listen

const (
// TIMEFORMATE 默认时间格式
TIMEFORMATE = "2006-01-02"
)

Expand Down Expand Up @@ -96,11 +97,6 @@ func GetRate() []*Listen {
r := make([]*Listen, 0, len(listenMap))
for p := range listenMap {
l := listenMap[p]
// rate := &Listen{
// Port: p,
// Rate: l.Rate,
// }

r = append(r, &l)
}

Expand Down
95 changes: 46 additions & 49 deletions shadowsocks/rate.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,53 @@
package shadowsocks

import "fmt"

/**
* Created with VScode.
* User: andy.zhangtao <[email protected]>
* Date: 16-10-11
* Time: 18:03
* 管理每个端口OUTPUT流量,以字节为单位
* Date: 16-10-13
* Time: 10:10
* 处理流量相关请求
*/

// Rate 流量类
// type Rate struct {
// Port string `json:"port"`
// Rate int `json:"rate"`
// }

// var rateMap map[string]int

// AddRate 按照端口统计流量
// func AddRate(r Rate) {
// if len(rateMap) == 0 {
// rateMap = make(map[string]int)
// }

// rateMap[r.Port] = rateMap[r.Port] + r.Rate
// }

// GetPortRate 获取指定端口流量
// func GetPortRate(port string) *Rate {
// r := &Rate{
// Port: port,
// Rate: rateMap[port],
// }

// return r
// }

// GetRate 获取所有端口流量
// func GetRate() []*Rate {
// r := make([]*Rate, 0, len(rateMap))
// for p := range rateMap {
// rate := &Rate{
// Port: p,
// Rate: rateMap[p],
// }

// r = append(r, rate)
// }

// return r
// }

// ClearPortRate 端口流量清零
// func ClearPortRate(port string) {
// rateMap[port] = 0
// }
// Rate 流量数据 每次都从Listen中重新获取数据
type Rate struct {
Port string `json:"port"`
Rate string `json:"rate"`
}

// ConvertRate 流量转换。 Btye --> KB --> MB --> GB
func ConvertRate(rate int) string {
if rate == 0 {
return "0KB"
}

count := 1
f := float64(rate)
f = f / 1024

for {

if isOK(f) {
break
}
count++
f = f / 1024
}

switch count {
case 1:
return fmt.Sprintf("%0.3f KB", f)
case 2:
return fmt.Sprintf("%0.3f MB", f)
case 3:
return fmt.Sprintf("%0.3f GB", f)
default:
return fmt.Sprintf("%0.3f Btye", f)
}
}

// isOK 判断当前流量值是否大于1024. 当大于1024时,返回false,继续换算。反之停止换算
func isOK(i float64) bool {
return i <= 1024
}

0 comments on commit 8d725d3

Please sign in to comment.