Skip to content

Commit

Permalink
Merge pull request #6 from gofrp/feat_refactor
Browse files Browse the repository at this point in the history
feat: refactor
  • Loading branch information
blizard863 authored Apr 15, 2024
2 parents dd5d858 + 34b5a9e commit d01b627
Show file tree
Hide file tree
Showing 24 changed files with 625 additions and 176 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## v0.1.2 (2024-04-15)

This release introduces a set of code optimizations, improvements to documentation, and an extension of the testing suite. This is a patch release and does not contain any breaking changes or new features. It is backwards compatible with v0.1.1.

### Improvements

**Code Optimization**: Refactored some parts of the codebase to enhance performance and readability.

**Documentation**: Updated and added inline documentation and comments to make the codebase more accessible for new contributors.

**Unit Testing**: Increased test coverage by adding new unit tests for previously untested functions.


## v0.1.1 (2024-04-12)
Init Project.

## v0.1.0 (2024-04-11)
Init Project.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ vet:
go vet ./...

gssh:
env CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -tags tiny-frpc -o bin/tiny-frpc ./cmd/go_ssh
env CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -tags gssh -o bin/tiny-frpc ./cmd/frpc

nssh:
env CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -tags tiny-frpc-ssh -o bin/tiny-frpc-ssh ./cmd/native_ssh
env CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -tags nssh -o bin/tiny-frpc-ssh ./cmd/frpc

test: gotest

Expand Down
4 changes: 2 additions & 2 deletions Makefile.cross-compiles
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ app:
gomips=$(shell echo "$(n)" | cut -d : -f 3);\
target_suffix=$${os}_$${arch};\
echo "Build $${os}-$${arch}...";\
env CGO_ENABLED=0 GOOS=$${os} GOARCH=$${arch} GOMIPS=$${gomips} go build -trimpath -ldflags "$(LDFLAGS)" -tags tiny-frpc -o ./release/tiny-frpc_$${target_suffix} ./cmd/go_ssh;\
env CGO_ENABLED=0 GOOS=$${os} GOARCH=$${arch} GOMIPS=$${gomips} go build -trimpath -ldflags "$(LDFLAGS)" -tags tiny-frpc-ssh -o ./release/tiny-frpc-ssh_$${target_suffix} ./cmd/native_ssh;\
env CGO_ENABLED=0 GOOS=$${os} GOARCH=$${arch} GOMIPS=$${gomips} go build -trimpath -ldflags "$(LDFLAGS)" -tags gssh -o ./release/tiny-frpc_$${target_suffix} ./cmd/frpc;\
env CGO_ENABLED=0 GOOS=$${os} GOARCH=$${arch} GOMIPS=$${gomips} go build -trimpath -ldflags "$(LDFLAGS)" -tags nssh -o ./release/tiny-frpc-ssh_$${target_suffix} ./cmd/frpc;\
echo "Build $${os}-$${arch} done";\
)
@mv ./release/tiny-frpc_windows_amd64 ./release/tiny-frpc_windows_amd64.exe
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ Assuming that the domain name 'test-tiny-frpc.frps.com' is resolved to the machi
> curl -v 'http://test-tiny-frpc.frps.com/'


# Principle
![how tiny frpc works](doc/pic/architecture.png)


# Disclaimer

**This is currently a preview version. Compatibility is not guaranteed. It is presently for testing purposes only and should not be used in production environments!**
3 changes: 3 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ HTTP 服务:
访问到内网的 HTTP 服务。


# 原理
![how tiny frpc works](doc/pic/architecture.png)


# 说明

Expand Down
15 changes: 1 addition & 14 deletions Release.md
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
### Features

Introducing version of our proxy solution, we enable reverse proxying through standard ssh protocol communicating with frps.

We offer two binary program options for different user needs:

The Go standalone mode: This version works independently to communicate with frps. It's built for those who favor self-sufficiency and prefer a simpler deployment process.

The ssh native mode: This version works in conjunction with the operating system's ssh program. It is intended for those whose systems comprise a preconfigured ssh setup, or who wish to utilize the existing ssh program.
Both versions necessitate the provision of a frpc toml format configuration file to function correctly.

This release is just the beginning. Stay tuned for more advanced features, improvements and we are always keen to hear user feedback for future developments.

**This is currently a preview version. Compatibility is not guaranteed. It is presently for testing purposes only and should not be used in production environments!!!**
Please refer to [CHANGELOG.md](https://github.com/gofrp/tiny-frpc/blob/main/CHANGELOG.md) for details.
24 changes: 24 additions & 0 deletions cmd/frpc/default_runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
v1 "github.com/gofrp/tiny-frpc/pkg/config/v1"
"github.com/gofrp/tiny-frpc/pkg/model"
"github.com/gofrp/tiny-frpc/pkg/util/log"
)

var runner model.Runner = defaultRunner{}

type defaultRunner struct{}

func (r defaultRunner) New(commonCfg *v1.ClientCommonConfig, pxyCfg []v1.ProxyConfigurer, vCfg []v1.VisitorConfigurer) (err error) {
log.Infof("init default runner")
return
}

func (r defaultRunner) Run() (err error) {
return
}

func (r defaultRunner) Close() (err error) {
return
}
96 changes: 96 additions & 0 deletions cmd/frpc/gssh_runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//go:build gssh
// +build gssh

package main

import (
"sync"

"github.com/gofrp/tiny-frpc/pkg/config"
v1 "github.com/gofrp/tiny-frpc/pkg/config/v1"
"github.com/gofrp/tiny-frpc/pkg/gssh"
"github.com/gofrp/tiny-frpc/pkg/util/log"
)

type GoSSHRun struct {
commonCfg *v1.ClientCommonConfig
pxyCfg []v1.ProxyConfigurer
vCfg []v1.VisitorConfigurer

wg *sync.WaitGroup
mu *sync.RWMutex

tcs map[int]*gssh.TunnelClient
}

func (gr *GoSSHRun) New(commonCfg *v1.ClientCommonConfig, pxyCfg []v1.ProxyConfigurer, vCfg []v1.VisitorConfigurer) error {
log.Infof("init go ssh runner")

runner = &GoSSHRun{
commonCfg: commonCfg,
pxyCfg: pxyCfg,
vCfg: vCfg,

wg: new(sync.WaitGroup),
mu: &sync.RWMutex{},

tcs: make(map[int]*gssh.TunnelClient, 0),
}
return nil
}

func (gr *GoSSHRun) Run() error {
params := config.ParseFRPCConfigToGoSSHParam(gr.commonCfg, gr.pxyCfg, gr.vCfg)

log.Infof("proxy total len: %v", len(params))

for i, cmd := range params {
gr.wg.Add(1)

go func(cmd config.GoSSHParam, idx int) {
defer gr.wg.Done()

log.Infof("start to run: %v", cmd)

tc, err := gssh.NewTunnelClient(cmd.LocalAddr, cmd.ServerAddr, cmd.SSHExtraCmd)
if err != nil {
log.Errorf("new ssh tunnel client error: %v", err)
return
}

gr.mu.Lock()
gr.tcs[idx] = tc
gr.mu.Unlock()

err = tc.Start()
if err != nil {
log.Errorf("cmd: %v run error: %v", cmd, err)

gr.mu.Lock()
delete(gr.tcs, idx)
gr.mu.Unlock()

return
}
}(cmd, i)
}

gr.wg.Wait()

log.Infof("stopping ssh tunnel to frps")
return nil
}

func (gr *GoSSHRun) Close() error {
gr.mu.Lock()
defer gr.mu.Unlock()

for _, tc := range gr.tcs {
tc.Close()
}
return nil
}

func init() {
runner = &GoSSHRun{}
}
49 changes: 25 additions & 24 deletions cmd/native_ssh/main.go → cmd/frpc/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package main

import (
"context"
"flag"
"fmt"
"sync"
"os"
"os/signal"
"syscall"
"time"

"github.com/gofrp/tiny-frpc/pkg/config"
v1 "github.com/gofrp/tiny-frpc/pkg/config/v1"
"github.com/gofrp/tiny-frpc/pkg/nssh"
"github.com/gofrp/tiny-frpc/pkg/model"
"github.com/gofrp/tiny-frpc/pkg/util"
"github.com/gofrp/tiny-frpc/pkg/util/log"
"github.com/gofrp/tiny-frpc/pkg/util/version"
Expand All @@ -20,7 +22,7 @@ func main() {
showVersion bool
)

flag.StringVar(&cfgFilePath, "c", "frpc.toml", "Path to the configuration file")
flag.StringVar(&cfgFilePath, "c", "frpc.toml", "path to the configuration file")
flag.BoolVar(&showVersion, "v", false, "version of tiny-frpc")
flag.Parse()

Expand All @@ -43,29 +45,28 @@ func main() {

log.Infof("common cfg: %v, proxy cfg: %v, visitor cfg: %v", util.JSONEncode(cfg), util.JSONEncode(proxyCfgs), util.JSONEncode(visitorCfgs))

sshCmds := config.ParseFRPCConfigToSSHCmd(cfg, proxyCfgs, visitorCfgs)

log.Infof("ssh cmds len_num: %v", len(sshCmds))

closeCh := make(chan struct{})
wg := new(sync.WaitGroup)

for _, cmd := range sshCmds {
wg.Add(1)

go func(cmd string) {
defer wg.Done()
ctx := context.Background()
err = runner.New(cfg, proxyCfgs, visitorCfgs)
if err != nil {
log.Errorf("new runner error: %v", err)
return
}

log.Infof("start to run: %v", cmd)
go handleTermSignal(runner)

task := nssh.NewCmdWrapper(ctx, cmd, closeCh)
task.ExecuteCommand(ctx)
}(cmd)
err = runner.Run()
if err != nil {
log.Errorf("run error: %v", err)
return
}

wg.Wait()
close(closeCh)
time.Sleep(time.Millisecond * 10)
log.Infof("process exit...")
}

log.Infof("stopping process calling native ssh to frps, exit...")
func handleTermSignal(run model.Runner) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
v := <-ch
log.Infof("get signal term: %v, gracefully shutdown", v)
run.Close()
}
87 changes: 87 additions & 0 deletions cmd/frpc/nssh_runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//go:build nssh
// +build nssh

package main

import (
"context"
"sync"

"github.com/gofrp/tiny-frpc/pkg/config"
v1 "github.com/gofrp/tiny-frpc/pkg/config/v1"
"github.com/gofrp/tiny-frpc/pkg/nssh"
"github.com/gofrp/tiny-frpc/pkg/util/log"
)

type NativeSSHRun struct {
commonCfg *v1.ClientCommonConfig
pxyCfg []v1.ProxyConfigurer
vCfg []v1.VisitorConfigurer

wg *sync.WaitGroup
mu *sync.RWMutex

cws map[int]*nssh.CmdWrapper
}

func (nr *NativeSSHRun) New(commonCfg *v1.ClientCommonConfig, pxyCfg []v1.ProxyConfigurer, vCfg []v1.VisitorConfigurer) error {
log.Infof("init native ssh runner")

runner = &NativeSSHRun{
commonCfg: commonCfg,
pxyCfg: pxyCfg,
vCfg: vCfg,

wg: new(sync.WaitGroup),
mu: &sync.RWMutex{},

cws: make(map[int]*nssh.CmdWrapper, 0),
}
return nil
}

func (nr *NativeSSHRun) Run() error {
cmdParams := config.ParseFRPCConfigToSSHCmd(nr.commonCfg, nr.pxyCfg, nr.vCfg)

log.Infof("proxy total len: %v", len(cmdParams))

for i, cmd := range cmdParams {
nr.wg.Add(1)

go func(cmd string, idx int) {
defer nr.wg.Done()
ctx := context.Background()

log.Infof("start to run: %v", cmd)

cmdWrapper := nssh.NewCmdWrapper(ctx, cmd)

nr.mu.Lock()
nr.cws[idx] = cmdWrapper
nr.mu.Unlock()

cmdWrapper.ExecuteCommand(ctx)
}(cmd, i)
}

nr.wg.Wait()

log.Infof("stopping native ssh tunnel to frps")

return nil
}

func (nr *NativeSSHRun) Close() error {
nr.mu.Lock()
defer nr.mu.Unlock()

for _, c := range nr.cws {
c.Close()
}

return nil
}

func init() {
runner = &NativeSSHRun{}
}
Loading

0 comments on commit d01b627

Please sign in to comment.