Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

Commit

Permalink
v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lzjluzijie committed May 12, 2018
2 parents e684a77 + 1534973 commit d4bb2b6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
3 changes: 3 additions & 0 deletions README-zh.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# WebSocks

[教程资源](https://zhuji.lu/tags/websocks)
[TG群](https://t.me/websocks)

一个基于 WebSocket 的代理工具

本项目目前还在开发中,更多功能仍在完善中。如果你对这个项目感兴趣,请star它来支持我,蟹蟹
Expand Down
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# WebSocks

[教程资源](https://zhuji.lu/tags/websocks)
[中文说明](https://github.com/lzjluzijie/websocks/blob/master/README-zh.md)
[Telegram Group](https://t.me/websocks)

A secure proxy based on websocket.

Expand All @@ -19,6 +21,19 @@ To-Do: WebSocks mux

## Example

### Built-in TLS with fake server name and reversing proxy

#### Server
```
./websocks cert
./websocks server -l :2333 -p /password --proxy https://www.centos.org/ --tls
```

#### Local
```
./websocks client -l :1080 -s wss://the-real-server.com:2333/password -n www.centos.com --insecure
```

### Caddy TLS

#### Server
Expand All @@ -39,16 +54,3 @@ https://server.com {
}
}
```

### Built-in TLS with fake server name and reversing proxy

#### Server
```
./websocks cert
./websocks server -l :2333 -p /password --proxy https://www.centos.org/ --tls
```

#### Local
```
./websocks client -l :1080 -s wss://the-real-server.com:2333/password -n www.centos.com --insecure
```
62 changes: 41 additions & 21 deletions core/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"net/http/httputil"
"net/url"

"sync/atomic"

"fmt"

"github.com/juju/loggo"
"golang.org/x/net/websocket"
)
Expand All @@ -23,67 +27,83 @@ type Server struct {
CertPath string
KeyPath string
Proxy string
}

var opened = 0
var closed = 0
var t time.Time
CreatedAt time.Time

func handler(ws *websocket.Conn) {
opened++
var err error
defer func() {
closed++
if err != nil {
logger.Debugf(err.Error())
}
}()
Opened uint64
Closed uint64
Uploaded uint64
Downloaded uint64
}

func (server *Server) HandleWebSocket(ws *websocket.Conn) {
server.Opened++

defer ws.Close()

dec := gob.NewDecoder(ws)
req := Request{}
err = dec.Decode(&req)
err := dec.Decode(&req)
if err != nil {
server.Closed++
if err != nil {
logger.Debugf(err.Error())
}
return
}

logger.Debugf("Dial %s from %s", req.Addr, ws.RemoteAddr().String())
conn, err := net.Dial("tcp", req.Addr)
if err != nil {
server.Closed++
if err != nil {
logger.Debugf(err.Error())
}
return
}

defer conn.Close()

go func() {
_, err = io.Copy(conn, ws)
downloaded, err := io.Copy(conn, ws)
atomic.AddUint64(&server.Downloaded, uint64(downloaded))
if err != nil {
logger.Debugf(err.Error())
server.Closed++
if err != nil {
logger.Debugf(err.Error())
}
return
}
}()

_, err = io.Copy(ws, conn)
uploaded, err := io.Copy(ws, conn)
atomic.AddUint64(&server.Uploaded, uint64(uploaded))
if err != nil {
server.Closed++
if err != nil {
logger.Debugf(err.Error())
}
return
}
}

func (server *Server) Status(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("%ds: opened %d, closed %d, uploaded %d bytes, downloaded %d bytes", int(time.Since(server.CreatedAt).Seconds()), server.Opened, server.Closed, server.Uploaded, server.Downloaded)))
}

func (server *Server) Listen() (err error) {
logger.SetLogLevel(server.LogLevel)

t = time.Now()

go func() {
for {
time.Sleep(time.Second)
logger.Debugf("%ds: opened%d, closed%d", int(time.Since(t).Seconds()), opened, closed)
logger.Debugf("%ds: opened %d, closed %d, uploaded %d bytes, downloaded %d bytes", int(time.Since(server.CreatedAt).Seconds()), server.Opened, server.Closed, server.Uploaded, server.Downloaded)
}
}()

mux := http.NewServeMux()
mux.Handle(server.Pattern, websocket.Handler(handler))
mux.Handle(server.Pattern, websocket.Handler(server.HandleWebSocket))
mux.HandleFunc("/status", server.Status)
if server.Proxy != "" {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
remote, err := url.Parse(server.Proxy)
Expand Down
5 changes: 4 additions & 1 deletion websocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"io/ioutil"

"time"

"github.com/juju/loggo"
"github.com/lzjluzijie/websocks/core"
"github.com/urfave/cli"
Expand All @@ -20,7 +22,7 @@ func main() {

app := cli.NewApp()
app.Name = "WebSocks"
app.Version = "0.3.0"
app.Version = "0.4.0"
app.Usage = "A secure proxy based on WebSocket."
app.Description = "See https://github.com/lzjluzijie/websocks"
app.Author = "Halulu"
Expand Down Expand Up @@ -159,6 +161,7 @@ func main() {
CertPath: certPath,
KeyPath: keyPath,
Proxy: proxy,
CreatedAt: time.Now(),
}

logger.Infof("Listening at %s", listenAddr)
Expand Down

0 comments on commit d4bb2b6

Please sign in to comment.