Skip to content

Commit

Permalink
add remoteAddr in NewProxyResp message
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier committed Jan 17, 2018
1 parent 9a5f0c2 commit 3f6799c
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 60 deletions.
6 changes: 4 additions & 2 deletions client/admin_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,24 @@ func NewProxyStatusResp(status *ProxyStatus) ProxyStatusResp {
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
}
psr.Plugin = cfg.Plugin
psr.RemoteAddr = fmt.Sprintf(":%d", cfg.RemotePort)
psr.RemoteAddr = config.ClientCommonCfg.ServerAddr + status.RemoteAddr
case *config.UdpProxyConf:
if cfg.LocalPort != 0 {
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
}
psr.RemoteAddr = fmt.Sprintf(":%d", cfg.RemotePort)
psr.RemoteAddr = config.ClientCommonCfg.ServerAddr + status.RemoteAddr
case *config.HttpProxyConf:
if cfg.LocalPort != 0 {
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
}
psr.Plugin = cfg.Plugin
psr.RemoteAddr = status.RemoteAddr
case *config.HttpsProxyConf:
if cfg.LocalPort != 0 {
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
}
psr.Plugin = cfg.Plugin
psr.RemoteAddr = status.RemoteAddr
case *config.StcpProxyConf:
if cfg.LocalPort != 0 {
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
Expand Down
2 changes: 1 addition & 1 deletion client/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (ctl *Control) HandleReqWorkConn(inMsg *msg.ReqWorkConn) {
func (ctl *Control) HandleNewProxyResp(inMsg *msg.NewProxyResp) {
// Server will return NewProxyResp message to each NewProxy message.
// Start a new proxy handler if no error got
err := ctl.pm.StartProxy(inMsg.ProxyName, inMsg.Error)
err := ctl.pm.StartProxy(inMsg.ProxyName, inMsg.RemoteAddr, inMsg.Error)
if err != nil {
ctl.Warn("[%s] start error: %v", inMsg.ProxyName, err)
} else {
Expand Down
25 changes: 17 additions & 8 deletions client/proxy_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type ProxyWrapper struct {
Err string
Cfg config.ProxyConf

RemoteAddr string

pxy Proxy

mu sync.RWMutex
Expand All @@ -52,6 +54,9 @@ type ProxyStatus struct {
Status string `json:"status"`
Err string `json:"err"`
Cfg config.ProxyConf `json:"cfg"`

// Got from server.
RemoteAddr string `json:"remote_addr"`
}

func NewProxyWrapper(cfg config.ProxyConf) *ProxyWrapper {
Expand All @@ -78,16 +83,17 @@ func (pw *ProxyWrapper) GetStatus() *ProxyStatus {
pw.mu.RLock()
defer pw.mu.RUnlock()
ps := &ProxyStatus{
Name: pw.Name,
Type: pw.Type,
Status: pw.Status,
Err: pw.Err,
Cfg: pw.Cfg,
Name: pw.Name,
Type: pw.Type,
Status: pw.Status,
Err: pw.Err,
Cfg: pw.Cfg,
RemoteAddr: pw.RemoteAddr,
}
return ps
}

func (pw *ProxyWrapper) Start(serverRespErr string) error {
func (pw *ProxyWrapper) Start(remoteAddr string, serverRespErr string) error {
if pw.pxy != nil {
pw.pxy.Close()
pw.pxy = nil
Expand All @@ -96,6 +102,7 @@ func (pw *ProxyWrapper) Start(serverRespErr string) error {
if serverRespErr != "" {
pw.mu.Lock()
pw.Status = ProxyStatusStartErr
pw.RemoteAddr = remoteAddr
pw.Err = serverRespErr
pw.mu.Unlock()
return fmt.Errorf(serverRespErr)
Expand All @@ -104,6 +111,7 @@ func (pw *ProxyWrapper) Start(serverRespErr string) error {
pxy := NewProxy(pw.Cfg)
pw.mu.Lock()
defer pw.mu.Unlock()
pw.RemoteAddr = remoteAddr
if err := pxy.Run(); err != nil {
pw.Status = ProxyStatusStartErr
pw.Err = err.Error()
Expand Down Expand Up @@ -139,6 +147,7 @@ func (pw *ProxyWrapper) Close() {

func NewProxyManager(ctl *Control, msgSendCh chan (msg.Message), logPrefix string) *ProxyManager {
return &ProxyManager{
ctl: ctl,
proxies: make(map[string]*ProxyWrapper),
visitorCfgs: make(map[string]config.ProxyConf),
visitors: make(map[string]Visitor),
Expand Down Expand Up @@ -168,7 +177,7 @@ func (pm *ProxyManager) sendMsg(m msg.Message) error {
return err
}

func (pm *ProxyManager) StartProxy(name string, serverRespErr string) error {
func (pm *ProxyManager) StartProxy(name string, remoteAddr string, serverRespErr string) error {
pm.mu.Lock()
defer pm.mu.Unlock()
if pm.closed {
Expand All @@ -180,7 +189,7 @@ func (pm *ProxyManager) StartProxy(name string, serverRespErr string) error {
return fmt.Errorf("no proxy found")
}

if err := pxy.Start(serverRespErr); err != nil {
if err := pxy.Start(remoteAddr, serverRespErr); err != nil {
errRet := err
err = pm.sendMsg(&msg.CloseProxy{
ProxyName: name,
Expand Down
5 changes: 3 additions & 2 deletions models/msg/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ type NewProxy struct {
}

type NewProxyResp struct {
ProxyName string `json:"proxy_name"`
Error string `json:"error"`
ProxyName string `json:"proxy_name"`
RemoteAddr string `json:"remote_addr"`
Error string `json:"error"`
}

type CloseProxy struct {
Expand Down
17 changes: 9 additions & 8 deletions server/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,15 @@ func (ctl *Control) manager() {
switch m := rawMsg.(type) {
case *msg.NewProxy:
// register proxy in this control
err := ctl.RegisterProxy(m)
remoteAddr, err := ctl.RegisterProxy(m)
resp := &msg.NewProxyResp{
ProxyName: m.ProxyName,
}
if err != nil {
resp.Error = err.Error()
ctl.conn.Warn("new proxy [%s] error: %v", m.ProxyName, err)
} else {
resp.RemoteAddr = remoteAddr
ctl.conn.Info("new proxy [%s] success", m.ProxyName)
StatsNewProxy(m.ProxyName, m.ProxyType)
}
Expand All @@ -332,24 +333,24 @@ func (ctl *Control) manager() {
}
}

func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (err error) {
func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (remoteAddr string, err error) {
var pxyConf config.ProxyConf
// Load configures from NewProxy message and check.
pxyConf, err = config.NewProxyConf(pxyMsg)
if err != nil {
return err
return
}

// NewProxy will return a interface Proxy.
// In fact it create different proxies by different proxy type, we just call run() here.
pxy, err := NewProxy(ctl, pxyConf)
if err != nil {
return err
return remoteAddr, err
}

err = pxy.Run()
remoteAddr, err = pxy.Run()
if err != nil {
return err
return
}
defer func() {
if err != nil {
Expand All @@ -359,13 +360,13 @@ func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (err error) {

err = ctl.svr.RegisterProxy(pxyMsg.ProxyName, pxy)
if err != nil {
return err
return
}

ctl.mu.Lock()
ctl.proxies[pxy.GetName()] = pxy
ctl.mu.Unlock()
return nil
return
}

func (ctl *Control) CloseProxy(closeMsg *msg.CloseProxy) (err error) {
Expand Down
Loading

0 comments on commit 3f6799c

Please sign in to comment.