Skip to content

Commit

Permalink
refactor: client (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
DVKunion authored Jan 17, 2024
1 parent 75db770 commit f40e8f0
Show file tree
Hide file tree
Showing 52 changed files with 250 additions and 390 deletions.
2 changes: 1 addition & 1 deletion .github/conf/.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ builds:
- binary: client
env:
- CGO_ENABLED=0
main: ./main.go
main: ./cmd/main.go
goos:
- linux
- windows
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ jobs:
with:
go-version: 1.21
- name: Build
run: go build --ldflags "-s -w -X github.com/DVKunion/SeaMoon/pkg/consts.Version=${{github.ref_name}}" main.go
run: go build --ldflags "-s -w -X github.com/DVKunion/SeaMoon/pkg/consts.Version=${{github.ref_name}}" cmd/main.go

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ COPY .. /src
WORKDIR /src
ENV CGO_ENABLED 0
ENV VERSION=${VERSION}
RUN go build -ldflags "-X github.com/DVKunion/SeaMoon/server/consts.Version=${VERSION}" -o /tmp/seamoon main.go
RUN go build -ldflags "-X github.com/DVKunion/SeaMoon/server/consts.Version=${VERSION}" -o /tmp/seamoon cmd/main.go
RUN chmod +x /tmp/seamoon
# run stage
FROM alpine:3.19
Expand Down
24 changes: 6 additions & 18 deletions pkg/client/client.go → cmd/client/client.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
package client

import (
"bufio"
"context"
"html/template"
"io"
"log/slog"
"net"
"net/http"
"os"

"github.com/gin-gonic/gin"

"github.com/DVKunion/SeaMoon/pkg/consts"
"github.com/DVKunion/SeaMoon/static"

_ "net/http/pprof"
)

type Client struct {
net.Conn
br *bufio.Reader
}

func (c *Client) Read(b []byte) (int, error) {
return c.br.Read(b)
}
"github.com/DVKunion/SeaMoon/cmd/client/static"
"github.com/DVKunion/SeaMoon/pkg/consts"
)

func Serve(ctx context.Context, verbose bool, debug bool) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
sg := NewSigGroup()
go Controller(sg, verbose, debug)
go HttpController(ctx, sg)
go Socks5Controller(ctx, sg)
go API(sg, verbose, debug)
go Control(ctx, sg)

Config().Load(sg)
<-sg.WatchChannel
Expand All @@ -44,7 +32,7 @@ func Serve(ctx context.Context, verbose bool, debug bool) {
sg.wg.Wait()
}

func Controller(sg *SigGroup, verbose bool, debug bool) {
func API(sg *SigGroup, verbose bool, debug bool) {
slog.Info(consts.CONTROLLER_START, "addr", Config().Control.ConfigAddr)

if consts.Version != "dev" || !debug {
Expand Down
11 changes: 11 additions & 0 deletions pkg/client/config.go → cmd/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/BurntSushi/toml"

"github.com/DVKunion/SeaMoon/pkg/consts"
"github.com/DVKunion/SeaMoon/pkg/transfer"
)

type clientConfig struct {
Expand All @@ -17,6 +18,16 @@ type clientConfig struct {
Socks5 proxyConfig `toml:"socks5"`
}

func (c *clientConfig) Addr(t transfer.Type) string {
switch t {
case transfer.HTTP:
return c.Http.ListenAddr
case transfer.SOCKS5:
return c.Socks5.ListenAddr
}
return ""
}

type controlConfig struct {
ConfigAddr string `toml:"addr"`
LogPath string `toml:"logPath"`
Expand Down
83 changes: 83 additions & 0 deletions cmd/client/control.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package client

import (
"context"
"errors"
"log/slog"
"net"
"strings"

"github.com/DVKunion/SeaMoon/pkg/consts"
"github.com/DVKunion/SeaMoon/pkg/network"
"github.com/DVKunion/SeaMoon/pkg/service"
"github.com/DVKunion/SeaMoon/pkg/transfer"
"github.com/DVKunion/SeaMoon/pkg/tunnel"
)

func Control(ctx context.Context, sg *SigGroup) {
c, cancel := context.WithCancel(ctx)
defer cancel()
for {
select {
case t := <-sg.StartChannel:
slog.Info(consts.LISTEN_START, "type", t)
sg.wg.Add(1)
go func() {
if err := doListen(c, t); err != nil {
slog.Error(consts.LISTEN_ERROR, "type", t, "err", err)
}
}()
sg.wg.Done()
case t := <-sg.StopChannel:
slog.Info(consts.LISTEN_STOP, "type", t)
cancel()
}
}
}

func doListen(ctx context.Context, t transfer.Type) error {
server, err := net.Listen("tcp", Config().Addr(t))
if err != nil {
return err
}
var proxyAddr string
var proxyType tunnel.Type
for _, p := range Config().ProxyAddr {
if strings.HasPrefix(p, "ws://") {
proxyAddr = strings.TrimPrefix(p, "ws://")
proxyType = tunnel.WST
break
}
if strings.HasPrefix(p, "grpc://") {
proxyAddr = p
proxyType = tunnel.GRT
break
}
}
if proxyAddr == "" || proxyType == "" {
return errors.New(consts.PROXY_ADDR_ERROR)
}
go listen(ctx, server, proxyAddr, proxyType, t)
<-ctx.Done()
return nil
}

func listen(ctx context.Context, server net.Listener, pa string, pt tunnel.Type, t transfer.Type) {
defer server.Close()
for {
conn, err := server.Accept()
if err != nil {
slog.Error(consts.ACCEPT_ERROR, "err", err)
}
if srv, ok := service.Factory[pt]; ok {
destConn, err := srv.Conn(ctx, t, service.WithAddr(pa))
if err != nil {
slog.Error(consts.CONNECT_RMOET_ERROR, "err", err)
continue
}
if err := network.Transport(conn, destConn); err != nil {
slog.Error(consts.CONNECT_TRANS_ERROR, "err", err)
}
}
}
}
33 changes: 11 additions & 22 deletions pkg/client/signal.go → cmd/client/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,23 @@ import (
"os/signal"
"sync"
"syscall"
)

type ControlSignal int8

const (
HttpProxyStartSignal ControlSignal = iota + 1
HttpProxyStopSignal
SocksProxyStartSignal
SocksProxyStopSignal
"github.com/DVKunion/SeaMoon/pkg/transfer"
)

type SigGroup struct {
wg *sync.WaitGroup
WatchChannel chan os.Signal
HttpStartChannel chan ControlSignal
HttpStopChannel chan ControlSignal
SocksStartChannel chan ControlSignal
SocksStopChannel chan ControlSignal
wg *sync.WaitGroup
WatchChannel chan os.Signal
StartChannel chan transfer.Type
StopChannel chan transfer.Type
}

func NewSigGroup() *SigGroup {
sg := &SigGroup{
new(sync.WaitGroup),
make(chan os.Signal, 1),
make(chan ControlSignal, 1),
make(chan ControlSignal, 1),
make(chan ControlSignal, 1),
make(chan ControlSignal, 1),
make(chan transfer.Type, 1),
make(chan transfer.Type, 1),
}
signal.Notify(sg.WatchChannel, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
return sg
Expand All @@ -42,31 +31,31 @@ func (sg *SigGroup) StartHttpProxy() {
if Config().Http.Status == "active" {
return
}
sg.HttpStartChannel <- HttpProxyStartSignal
sg.StartChannel <- transfer.HTTP
Config().Http.Status = "active"
}

func (sg *SigGroup) StopHttpProxy() {
if Config().Http.Status == "inactive" {
return
}
sg.HttpStopChannel <- HttpProxyStopSignal
sg.StopChannel <- transfer.HTTP
Config().Http.Status = "inactive"
}

func (sg *SigGroup) StartSocksProxy() {
if Config().Socks5.Status == "active" {
return
}
sg.SocksStartChannel <- SocksProxyStartSignal
sg.StartChannel <- transfer.SOCKS5
Config().Socks5.Status = "active"
}

func (sg *SigGroup) StopSocksProxy() {
if Config().Socks5.Status == "inactive" {
return
}
sg.SocksStopChannel <- SocksProxyStopSignal
sg.StopChannel <- transfer.SOCKS5
Config().Socks5.Status = "inactive"
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
font-family: 'Lato';
font-style: normal;
font-weight: 400;
src: url(/static/public/css/S6uyw4BMUTPHjx4wXiWtFCc.woff2) format('woff2');
src: url(S6uyw4BMUTPHjx4wXiWtFCc.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

Expand All @@ -12,7 +12,7 @@
font-family: 'Lato';
font-style: normal;
font-weight: 700;
src: url(/static/public/css/S6u9w4BMUTPHh6UVSwiPGQ3q5d0.woff2) format('woff2');
src: url(S6u9w4BMUTPHh6UVSwiPGQ3q5d0.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ <h4 class="ui inverted dividing header"><i class="paper plane icon"></i>代理
</div>
</div>
<h4 class="ui dividing inverted header"><i class="ellipsis horizontal icon"></i>高级功能</h4>
<div class="inline field">
<div class="ui checkbox">
<input type="checkbox" tabindex="0" class="hidden">
<label>开启webshell动态代理功能(待实现)</label>
</div>
</div>
{{/* <div class="inline field">*/}}
{{/* <div class="ui checkbox">*/}}
{{/* <input type="checkbox" tabindex="0" class="hidden">*/}}
{{/* <label>开启webshell动态代理功能(待实现)</label>*/}}
{{/* </div>*/}}
{{/* </div>*/}}
<!-- <div class="inline field">-->
<!-- <div class="ui checkbox">-->
<!-- <input type="checkbox" tabindex="0" class="hidden">-->
Expand Down
4 changes: 2 additions & 2 deletions main.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (

"github.com/spf13/cobra"

"github.com/DVKunion/SeaMoon/pkg/client"
"github.com/DVKunion/SeaMoon/cmd/client"
"github.com/DVKunion/SeaMoon/cmd/server"
"github.com/DVKunion/SeaMoon/pkg/consts"
"github.com/DVKunion/SeaMoon/server"
)

var (
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion server/server.go → cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

net "github.com/DVKunion/SeaMoon/pkg/network"
"github.com/DVKunion/SeaMoon/server/service"
"github.com/DVKunion/SeaMoon/pkg/service"
)

type Server struct {
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ go 1.21
require (
github.com/BurntSushi/toml v1.3.2
github.com/gin-gonic/gin v1.9.1
github.com/google/martian/v3 v3.3.2
github.com/gorilla/websocket v1.5.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.0
github.com/tg123/go-htpasswd v1.2.0
github.com/xtaci/smux v1.5.24
Expand All @@ -26,7 +24,6 @@ require (
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
Expand All @@ -63,8 +60,6 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian/v3 v3.3.2 h1:IqNFLAmvJOgVlpdEBiQbDc2EwKW77amAycfTuWKdfvw=
github.com/google/martian/v3 v3.3.2/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
Expand All @@ -86,8 +81,6 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down Expand Up @@ -131,7 +124,6 @@ golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -164,7 +156,6 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.37.0 h1:uSZWeQJX5j11bIQ4AJoj+McDBo29cY1MCoC1wO3ts+c=
google.golang.org/grpc v1.37.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand Down
Loading

0 comments on commit f40e8f0

Please sign in to comment.