frp/src/frp/models/client/client.go

94 lines
2.3 KiB
Go
Raw Normal View History

2016-03-14 03:18:24 +00:00
// Copyright 2016 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
2016-01-27 13:24:36 +00:00
import (
"encoding/json"
"fmt"
"time"
2016-01-27 13:24:36 +00:00
"frp/models/config"
"frp/models/consts"
"frp/models/msg"
"frp/utils/conn"
"frp/utils/log"
"frp/utils/pcrypto"
2016-01-27 13:24:36 +00:00
)
type ProxyClient struct {
config.BaseConf
LocalIp string
LocalPort int64
2016-01-27 13:24:36 +00:00
}
func (p *ProxyClient) GetLocalConn() (c *conn.Conn, err error) {
c, err = conn.ConnectServer(p.LocalIp, p.LocalPort)
2016-01-27 13:24:36 +00:00
if err != nil {
log.Error("ProxyName [%s], connect to local port error, %v", p.Name, err)
}
return
}
func (p *ProxyClient) GetRemoteConn(addr string, port int64) (c *conn.Conn, err error) {
2016-02-03 10:46:24 +00:00
defer func() {
2016-01-27 13:24:36 +00:00
if err != nil {
c.Close()
}
}()
c, err = conn.ConnectServer(addr, port)
2016-01-27 13:24:36 +00:00
if err != nil {
log.Error("ProxyName [%s], connect to server [%s:%d] error, %v", p.Name, addr, port, err)
return
}
nowTime := time.Now().Unix()
authKey := pcrypto.GetAuthKey(p.Name + p.AuthToken + fmt.Sprintf("%d", nowTime))
2016-03-31 10:03:44 +00:00
req := &msg.ControlReq{
Type: consts.NewWorkConn,
2016-02-03 10:46:24 +00:00
ProxyName: p.Name,
AuthKey: authKey,
Timestamp: nowTime,
2016-01-27 13:24:36 +00:00
}
buf, _ := json.Marshal(req)
err = c.Write(string(buf) + "\n")
if err != nil {
log.Error("ProxyName [%s], write to server error, %v", p.Name, err)
return
}
err = nil
return
}
func (p *ProxyClient) StartTunnel(serverAddr string, serverPort int64) (err error) {
localConn, err := p.GetLocalConn()
if err != nil {
return
}
remoteConn, err := p.GetRemoteConn(serverAddr, serverPort)
if err != nil {
return
}
// l means local, r means remote
2016-03-31 10:03:44 +00:00
log.Debug("Join two connections, (l[%s] r[%s]) (l[%s] r[%s])", localConn.GetLocalAddr(), localConn.GetRemoteAddr(),
2016-02-03 10:46:24 +00:00
remoteConn.GetLocalAddr(), remoteConn.GetRemoteAddr())
go msg.JoinMore(localConn, remoteConn, p.BaseConf)
2016-01-27 13:24:36 +00:00
return nil
}