Skip to content
This repository has been archived by the owner on Jun 16, 2019. It is now read-only.

Commit

Permalink
Merge pull request #35 from orvice/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
orvice committed Apr 8, 2016
2 parents 6238922 + 517b487 commit 6be75ab
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ install:
- go install ./mu
script:
- cd mu && go build
- go test -cover ./...
sudo: required
6 changes: 6 additions & 0 deletions mu/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ func boot() {
if err != nil {
Log.Error(err)
}

err = client.UpdateNodeInfo()
if err != nil {
Log.Error(err)
}

}()
time.Sleep(muconfig.Conf.Base.SyncTime * time.Second)
}
Expand Down
6 changes: 4 additions & 2 deletions mu/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
)

var (
debug bool
logPath string
debug bool
logPath string
configPath string
)

func InitFlag() {
// flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
flag.StringVar(&logPath, "log_path", "./ss.log", "log file path")
flag.StringVar(&configPath, "config_path", "./config.conf", "log file path")
flag.BoolVar(&debug, "debug", false, "debug")
flag.Parse()
}
3 changes: 2 additions & 1 deletion mu/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/Sirupsen/logrus"
"github.com/orvice/shadowsocks-go/mu/log"
"io"
"os"
)
Expand All @@ -22,5 +23,5 @@ func InitLog() {
Log.Level = logrus.DebugLevel
Log.Debug("debug on")
}

log.SetLogClient(Log)
}
11 changes: 11 additions & 0 deletions mu/log/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package log

import (
"github.com/orvice/shadowsocks-go/mu/user"
)

type Client interface {
Info(user user.User, args ...interface{})
Error(user user.User, args ...interface{})
Debug(user user.User, args ...interface{})
}
4 changes: 4 additions & 0 deletions mu/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ import (
var (
Log = logrus.New()
)

func SetLogClient(client *logrus.Logger) {
Log = client
}
4 changes: 4 additions & 0 deletions mu/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,7 @@ func (c *Client) GetUsers() ([]user.User, error) {
func (c *Client) LogNodeOnlineUser(onlineUserCount int) error {
return nil
}

func (c *Client) UpdateNodeInfo() error {
return nil
}
27 changes: 27 additions & 0 deletions mu/system/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package system

import (
"os/exec"
"strings"
)

type Client struct {
}

func GetLoad() (string, error) {
output, err := exec.Command("cat", "/proc/loadavg").Output()
if err != nil {
return "", err
}

return string(output), nil
}

func GetUptime() (string, error) {
output, err := exec.Command("cat", "/proc/uptime").Output()
if err != nil {
return "", err
}
loadAry := strings.Split(string(output), " ")
return loadAry[0], nil
}
21 changes: 21 additions & 0 deletions mu/system/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package system

import (
"testing"
)

func TestGetUptime(t *testing.T) {
output, err := GetUptime()
if err != nil {
t.Error(err)
}
t.Log(output)
}

func TestGetLoad(t *testing.T) {
output, err := GetLoad()
if err != nil {
t.Error(err)
}
t.Log(output)
}
1 change: 1 addition & 0 deletions mu/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func SetClient(c Client) {
type Client interface {
GetUsers() ([]User, error)
LogNodeOnlineUser(onlineUserCount int) error
UpdateNodeInfo() error
}

type User interface {
Expand Down
31 changes: 30 additions & 1 deletion mu/webapi/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"github.com/orvice/shadowsocks-go/mu/log"
"github.com/orvice/shadowsocks-go/mu/system"
"github.com/orvice/shadowsocks-go/mu/user"
)

Expand Down Expand Up @@ -75,8 +76,9 @@ func (c *Client) UpdateTraffic(userId int, u, d string) error {
return err
}
if ret.Ret == 0 {
return UpdateTrafficFail
return errors.New(ret.Msg)
}
log.Log.Debug("update traffic debug:", ret.Msg)
return nil
}

Expand All @@ -95,3 +97,30 @@ func (c *Client) LogNodeOnlineUser(onlineUserCount int) error {
}
return nil
}

func (c *Client) UpdateNodeInfo() error {
uptime, err := system.GetUptime()
if err != nil {
log.Log.Error(err)
uptime = "0"
}

load, err := system.GetLoad()
if err != nil {
load = "0 0 0"
}

res, err := c.httpPostNodeInfo(load, uptime)
if err != nil {
return nil
}
var ret BaseRet
err = json.Unmarshal([]byte(res), &ret)
if err != nil {
return err
}
if ret.Ret == 0 {
return UpdateOnlineCountFail
}
return nil
}
21 changes: 21 additions & 0 deletions mu/webapi/web_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (c *Client) genNodeOnlineCountUrl(id int) string {
return fmt.Sprintf("%s/nodes/%d/online_count?key=%s", c.baseUrl, id, c.key)
}

func (c *Client) genNodeInfoUrl(id int) string {
return fmt.Sprintf("%s/nodes/%d/info?key=%s", c.baseUrl, id, c.key)
}

func (c *Client) httpGet(urlStr string) (string, error) {
resp, err := http.Get(urlStr)
if err != nil {
Expand Down Expand Up @@ -68,3 +72,20 @@ func (c *Client) httpPostNodeOnlineCount(count int) (string, error) {
}
return string(body), nil
}

func (c *Client) httpPostNodeInfo(load, uptime string) (string, error) {
urlStr := c.genNodeInfoUrl(c.nodeId)
resp, err := http.PostForm(urlStr,
url.Values{"load": {load}, "uptime": {uptime}})

if err != nil {
return "", err
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}

0 comments on commit 6be75ab

Please sign in to comment.