Skip to content

Commit

Permalink
Merge pull request #2906 from fatedier/dev
Browse files Browse the repository at this point in the history
bump version
  • Loading branch information
fatedier authored Apr 22, 2022
2 parents 10f2620 + 6c65858 commit eb1e19a
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
builds:
- skip: true
checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_sha256_checksums.txt'
name_template: '{{ .ProjectName }}_sha256_checksums.txt'
algorithm: sha256
extra_files:
- glob: ./release/packages/*
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ file:
fmt:
go fmt ./...

vet:
go vet ./...

frps:
env CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -o bin/frps ./cmd/frps

Expand All @@ -37,7 +40,7 @@ e2e:
e2e-trace:
DEBUG=true LOG_LEVEL=trace ./hack/run-e2e.sh

alltest: gotest e2e
alltest: vet gotest e2e

clean:
rm -f ./bin/frpc
Expand Down
7 changes: 3 additions & 4 deletions Release.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
### New

* Support go http pprof.
* Added new parameter `config_dir` in frpc to run multiple client instances in one process.

### Improve
### Fix

* Change underlying TCP connection keepalive interval to 2 hours.
* Create new connection to server for `sudp` visitor when needed, to avoid frequent reconnections.
* Equal sign in environment variables causes parsing error.
2 changes: 1 addition & 1 deletion client/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (pxy *XTCPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
// Listen for clientConn's address and wait for visitor connection
lConn, err := net.ListenUDP("udp", laddr)
if err != nil {
xl.Error("listen on visitorConn's local adress error: %v", err)
xl.Error("listen on visitorConn's local address error: %v", err)
return
}
defer lConn.Close()
Expand Down
36 changes: 35 additions & 1 deletion client/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (
"crypto/tls"
"fmt"
"io"
"math/rand"
"net"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
Expand All @@ -36,11 +38,17 @@ import (
"github.com/fatedier/frp/pkg/util/util"
"github.com/fatedier/frp/pkg/util/version"
"github.com/fatedier/frp/pkg/util/xlog"
"github.com/fatedier/golib/crypto"
libdial "github.com/fatedier/golib/net/dial"

fmux "github.com/hashicorp/yamux"
)

func init() {
crypto.DefaultSalt = "frp"
rand.Seed(time.Now().UnixNano())
}

// Service is a client service.
type Service struct {
// uniq id got from frps, attach it in loginMsg
Expand Down Expand Up @@ -98,6 +106,21 @@ func (svr *Service) GetController() *Control {
func (svr *Service) Run() error {
xl := xlog.FromContextSafe(svr.ctx)

// set custom DNSServer
if svr.cfg.DNSServer != "" {
dnsAddr := svr.cfg.DNSServer
if !strings.Contains(dnsAddr, ":") {
dnsAddr += ":53"
}
// Change default dns server for frpc
net.DefaultResolver = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
return net.Dial("udp", dnsAddr)
},
}
}

// login to frps
for {
conn, session, err := svr.login()
Expand Down Expand Up @@ -333,7 +356,14 @@ func (svr *Service) ReloadConf(pxyCfgs map[string]config.ProxyConf, visitorCfgs
svr.visitorCfgs = visitorCfgs
svr.cfgMu.Unlock()

return svr.ctl.ReloadConf(pxyCfgs, visitorCfgs)
svr.ctlMu.RLock()
ctl := svr.ctl
svr.ctlMu.RUnlock()

if ctl != nil {
return svr.ctl.ReloadConf(pxyCfgs, visitorCfgs)
}
return nil
}

func (svr *Service) Close() {
Expand All @@ -342,8 +372,12 @@ func (svr *Service) Close() {

func (svr *Service) GracefulClose(d time.Duration) {
atomic.StoreUint32(&svr.exit, 1)

svr.ctlMu.RLock()
if svr.ctl != nil {
svr.ctl.GracefulClose(d)
}
svr.ctlMu.RUnlock()

svr.cancel()
}
8 changes: 0 additions & 8 deletions cmd/frpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,10 @@
package main

import (
"math/rand"
"time"

_ "github.com/fatedier/frp/assets/frpc"
"github.com/fatedier/frp/cmd/frpc/sub"

"github.com/fatedier/golib/crypto"
)

func main() {
crypto.DefaultSalt = "frp"
rand.Seed(time.Now().UnixNano())

sub.Execute()
}
61 changes: 39 additions & 22 deletions cmd/frpc/sub/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
package sub

import (
"context"
"fmt"
"io/fs"
"net"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"

Expand All @@ -41,6 +42,7 @@ const (

var (
cfgFile string
cfgDir string
showVersion bool

serverAddr string
Expand Down Expand Up @@ -72,15 +74,12 @@ var (
bindPort int

tlsEnable bool

kcpDoneCh chan struct{}
)

func init() {
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "./frpc.ini", "config file of frpc")
rootCmd.PersistentFlags().StringVarP(&cfgDir, "config_dir", "", "", "config directory, run one frpc service for each file in config directory")
rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frpc")

kcpDoneCh = make(chan struct{})
}

func RegisterCommonFlags(cmd *cobra.Command) {
Expand All @@ -104,6 +103,32 @@ var rootCmd = &cobra.Command{
return nil
}

// If cfgDir is not empty, run multiple frpc service for each config file in cfgDir.
// Note that it's only designed for testing. It's not guaranteed to be stable.
if cfgDir != "" {
var wg sync.WaitGroup
filepath.WalkDir(cfgDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return nil
}
if d.IsDir() {
return nil
}
wg.Add(1)
time.Sleep(time.Millisecond)
go func() {
defer wg.Done()
err := runClient(path)
if err != nil {
fmt.Printf("frpc service error for config file [%s]\n", path)
}
}()
return nil
})
wg.Wait()
return nil
}

// Do not show command usage here.
err := runClient(cfgFile)
if err != nil {
Expand All @@ -120,12 +145,12 @@ func Execute() {
}
}

func handleSignal(svr *client.Service) {
ch := make(chan os.Signal)
func handleSignal(svr *client.Service, doneCh chan struct{}) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
svr.GracefulClose(500 * time.Millisecond)
close(kcpDoneCh)
close(doneCh)
}

func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) {
Expand Down Expand Up @@ -182,28 +207,20 @@ func startService(
log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel,
cfg.LogMaxDays, cfg.DisableLogColor)

if cfg.DNSServer != "" {
s := cfg.DNSServer
if !strings.Contains(s, ":") {
s += ":53"
}
// Change default dns server for frpc
net.DefaultResolver = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
return net.Dial("udp", s)
},
}
if cfgFile != "" {
log.Trace("start frpc service for config file [%s]", cfgFile)
defer log.Trace("frpc service for config file [%s] stopped", cfgFile)
}
svr, errRet := client.NewService(cfg, pxyCfgs, visitorCfgs, cfgFile)
if errRet != nil {
err = errRet
return
}

kcpDoneCh := make(chan struct{})
// Capture the exit signal if we use kcp.
if cfg.Protocol == "kcp" {
go handleSignal(svr)
go handleSignal(svr, kcpDoneCh)
}

err = svr.Run()
Expand Down
2 changes: 1 addition & 1 deletion conf/frpc_full.ini
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ tls_enable = true
# specify a dns server, so frpc will use this instead of default one
# dns_server = 8.8.8.8

# proxy names you want to start seperated by ','
# proxy names you want to start separated by ','
# default is empty, means all proxies
# start = ssh,dns

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/leodido/go-urn v1.2.1 // indirect
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.13.0
github.com/pires/go-proxyproto v0.5.0
github.com/pires/go-proxyproto v0.6.2
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
github.com/prometheus/client_golang v1.11.0
github.com/rodaine/table v1.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ github.com/onsi/gomega v1.13.0/go.mod h1:lRk9szgn8TxENtWd0Tp4c3wjlRfMTMH27I+3Je4
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pires/go-proxyproto v0.5.0 h1:A4Jv4ZCaV3AFJeGh5mGwkz4iuWUYMlQ7IoO/GTuSuLo=
github.com/pires/go-proxyproto v0.5.0/go.mod h1:Odh9VFOZJCf9G8cLW5o435Xf1J95Jw9Gw5rnCjcwzAY=
github.com/pires/go-proxyproto v0.6.2 h1:KAZ7UteSOt6urjme6ZldyFm4wDe/z0ZUP0Yv0Dos0d8=
github.com/pires/go-proxyproto v0.6.2/go.mod h1:Odh9VFOZJCf9G8cLW5o435Xf1J95Jw9Gw5rnCjcwzAY=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ type ClientCommonConf struct {
// It only works when "tls_enable" is valid and tls configuration of server
// has been specified.
TLSTrustedCaFile string `ini:"tls_trusted_ca_file" json:"tls_trusted_ca_file"`
// TLSServerName specifices the custom server name of tls certificate. By
// TLSServerName specifies the custom server name of tls certificate. By
// default, server name if same to ServerAddr.
TLSServerName string `ini:"tls_server_name" json:"tls_server_name"`
// By default, frpc will connect frps with first custom byte if tls is enabled.
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func init() {
glbEnvs = make(map[string]string)
envs := os.Environ()
for _, env := range envs {
kv := strings.Split(env, "=")
if len(kv) != 2 {
pair := strings.SplitN(env, "=", 2)
if len(pair) != 2 {
continue
}
glbEnvs[kv[0]] = kv[1]
glbEnvs[pair[0]] = pair[1]
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/util/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strings"
)

var version string = "0.41.0"
var version string = "0.42.0"

func Full() string {
return version
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/vhost/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func NewHTTPReverseProxy(option HTTPReverseProxyOptions, vhostRouter *Routers) *
BufferPool: newWrapPool(),
ErrorLog: log.New(newWrapLogger(), "", 0),
ErrorHandler: func(rw http.ResponseWriter, req *http.Request, err error) {
frpLog.Warn("do http proxy request error: %v", err)
frpLog.Warn("do http proxy request [host: %s] error: %v", req.Host, err)
rw.WriteHeader(http.StatusNotFound)
rw.Write(getNotFoundPageContent())
},
Expand Down
2 changes: 1 addition & 1 deletion server/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (ctl *Control) GetWorkConn() (workConn net.Conn, err error) {
case workConn, ok = <-ctl.workConnCh:
if !ok {
err = frpErr.ErrCtlClosed
xl.Warn("no work connections avaiable, %v", err)
xl.Warn("no work connections available, %v", err)
return
}

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/plugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ var _ = Describe("[Feature: Server-Plugins]", func() {
type = tcp
local_port = {{ .%s }}
remote_port = 0
`, framework.TCPEchoServerPort, remotePort)
`, framework.TCPEchoServerPort)

f.RunProcesses([]string{serverConf}, []string{clientConf})

Expand Down

0 comments on commit eb1e19a

Please sign in to comment.