-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
侯锐
committed
Jan 9, 2019
1 parent
0f66d51
commit bdd59d3
Showing
8 changed files
with
151 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package network | ||
|
||
import "log" | ||
|
||
// 当前所有的连接 | ||
var connections = make([]Connection, 50) | ||
|
||
func remove(conn Connection) { | ||
for i, c := range connections { | ||
if c == conn { | ||
connections = append(connections[:i], connections[i+1:]...) | ||
return | ||
} | ||
} | ||
log.Printf("%s not in connections\n", conn) | ||
} | ||
|
||
func add(conn Connection) { | ||
connections = append(connections, conn) | ||
} | ||
|
||
func GetConnections() []Connection { | ||
return connections | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package http | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gorilla/websocket" | ||
"github.com/ritterhou/stinger/core/common" | ||
"github.com/ritterhou/stinger/local/socks" | ||
"log" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
var download, upload uint64 | ||
|
||
// 计算带宽以及流量 | ||
func bandwidthTraffic() { | ||
log.Printf("Moniting bandwidth traffic.") | ||
|
||
ticker := time.NewTicker(1 * time.Second) | ||
lastDownload := socks.TotalDownload | ||
lastUpload := socks.TotalUpload | ||
for range ticker.C { | ||
t := time.Now() | ||
now := t.Format("2006-01-02 15:04:05") | ||
|
||
download = socks.TotalDownload - lastDownload | ||
upload = socks.TotalUpload - lastUpload | ||
if upload != 0 && download != 0 { | ||
fmt.Printf("%s %s ↓ %s ↑", now, common.ByteFormat(download), common.ByteFormat(upload)) | ||
fmt.Printf(" (%s ↓ %s ↑)\n", common.ByteFormat(socks.TotalDownload), common.ByteFormat(socks.TotalUpload)) | ||
} | ||
lastDownload = socks.TotalDownload | ||
lastUpload = socks.TotalUpload | ||
} | ||
} | ||
|
||
var upgrade = websocket.Upgrader{ | ||
ReadBufferSize: 1024, | ||
WriteBufferSize: 1024, | ||
} | ||
|
||
// 与网页端进行WebSocket连接 | ||
func ws(w http.ResponseWriter, req *http.Request) { | ||
conn, err := upgrade.Upgrade(w, req, nil) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
messageType, p, err := conn.ReadMessage() | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
separator := string(p) | ||
log.Println("The separator is", separator) | ||
|
||
ticker := time.NewTicker(1 * time.Second) | ||
lastDownload := download | ||
lastUpload := upload | ||
for range ticker.C { | ||
if lastDownload != download || lastUpload != upload { | ||
lastDownload = download | ||
lastUpload = upload | ||
message := fmt.Sprintf("%s%s%s", common.ByteFormat(download), separator, common.ByteFormat(upload)) | ||
if err := conn.WriteMessage(messageType, []byte(message)); err != nil { | ||
log.Println(err) | ||
conn.Close() | ||
break | ||
} | ||
} | ||
} | ||
log.Println("Stop sending traffic to", conn.RemoteAddr()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.