更新frp接口

This commit is contained in:
haidy 2018-11-26 22:28:48 +08:00
parent d29e84eb92
commit f3813c32a0
6 changed files with 48 additions and 5 deletions

View File

@ -24,12 +24,15 @@ type Service struct {
// manager control connection with server
ctl *Control
closedCh chan int
closedCh chan bool
closed bool
}
func NewService(pxyCfgs map[string]config.ProxyConf, visitorCfgs map[string]config.ProxyConf) (svr *Service) {
svr = &Service{
closedCh: make(chan int),
closedCh: make(chan bool),
closed: false,
}
ctl := NewControl(svr, pxyCfgs, visitorCfgs)
svr.ctl = ctl
@ -51,11 +54,11 @@ func (svr *Service) Run(cmd bool) error {
}
if cmd {
<-svr.closedCh
svr.closed = <-svr.closedCh
log.Info("svr closed")
} else {
go func() {
<-svr.closedCh
svr.closed = <-svr.closedCh
log.Info("svr closed")
}()
}
@ -64,5 +67,9 @@ func (svr *Service) Run(cmd bool) error {
func (svr *Service) Close() {
svr.ctl.Close()
svr.closedCh <- 0
svr.closedCh <- true
}
func (svr *Service) IsClosed() bool {
return svr.closed
}

View File

@ -30,12 +30,22 @@ func StopFrpc() (err error) {
return sub.StopFrp()
}
func IsFrpcRunning() bool {
return sub.IsFrpRunning()
}
func RunFrps(cfgFilePath string) (err error) {
crypto.DefaultSalt = "frp"
return frps.RunFrps(cfgFilePath)
}
// StopFrps 停止frps服务
func StopFrps() error {
return frps.StopFrps()
}
// IsFrpsRunning 是否还在运行
func IsFrpsRunning() bool {
return frps.IsFrpsRunning()
}

View File

@ -125,6 +125,10 @@ func StopFrp() (err error) {
return
}
func IsFrpRunning() bool {
return service != nil && !service.IsClosed()
}
func handleSignal(svr *client.Service) {
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)

View File

@ -233,3 +233,7 @@ func runBackgroundServer() (err error) {
func StopFrps() error {
return service.Stop()
}
func IsFrpsRunning() bool {
return service != nil && !service.Closed
}

View File

@ -82,6 +82,9 @@ type Service struct {
// Controller for nat hole connections
natHoleController *NatHoleController
// Closed is service closed
Closed bool
}
func NewService() (svr *Service, err error) {
@ -233,10 +236,18 @@ func (svr *Service) Run() {
}
func (svr *Service) Stop() error {
err := svr.muxer.Close()
if err != nil {
return err
}
return svr.listener.Close()
}
func (svr *Service) HandleListener(l frpNet.Listener) {
defer func() {
svr.Closed = true
log.Warn("Frps is Closed")
}()
// Listen for incoming connections from client.
for {
c, err := l.Accept()

View File

@ -51,6 +51,13 @@ func NewMux(ln net.Listener) (mux *Mux) {
return
}
func (mux *Mux) Close() error {
if mux.ln != nil {
return mux.ln.Close()
}
return nil
}
// priority
func (mux *Mux) Listen(priority int, needBytesNum uint32, fn MatchFunc) net.Listener {
ln := &listener{