Exemplo n.º 1
0
func loginToServer(cli *client.ProxyClient) (c *conn.Conn, err error) {
	c, err = conn.ConnectServer(client.ServerAddr, client.ServerPort)
	if err != nil {
		log.Error("ProxyName [%s], connect to server [%s:%d] error, %v", cli.Name, client.ServerAddr, client.ServerPort, err)
		return
	}

	nowTime := time.Now().Unix()
	req := &msg.ControlReq{
		Type:          consts.NewCtlConn,
		ProxyName:     cli.Name,
		UseEncryption: cli.UseEncryption,
		UseGzip:       cli.UseGzip,
		PrivilegeMode: cli.PrivilegeMode,
		ProxyType:     cli.Type,
		Timestamp:     nowTime,
	}
	if cli.PrivilegeMode {
		privilegeKey := pcrypto.GetAuthKey(cli.Name + client.PrivilegeToken + fmt.Sprintf("%d", nowTime))
		req.RemotePort = cli.RemotePort
		req.CustomDomains = cli.CustomDomains
		req.PrivilegeKey = privilegeKey
	} else {
		authKey := pcrypto.GetAuthKey(cli.Name + cli.AuthToken + fmt.Sprintf("%d", nowTime))
		req.AuthKey = authKey
	}

	buf, _ := json.Marshal(req)
	err = c.Write(string(buf) + "\n")
	if err != nil {
		log.Error("ProxyName [%s], write to server error, %v", cli.Name, err)
		return
	}

	res, err := c.ReadLine()
	if err != nil {
		log.Error("ProxyName [%s], read from server error, %v", cli.Name, err)
		return
	}
	log.Debug("ProxyName [%s], read [%s]", cli.Name, res)

	ctlRes := &msg.ControlRes{}
	if err = json.Unmarshal([]byte(res), &ctlRes); err != nil {
		log.Error("ProxyName [%s], format server response error, %v", cli.Name, err)
		return
	}

	if ctlRes.Code != 0 {
		log.Error("ProxyName [%s], start proxy error, %s", cli.Name, ctlRes.Msg)
		return c, fmt.Errorf("%s", ctlRes.Msg)
	}

	log.Debug("ProxyName [%s], connect to server [%s:%d] success!", cli.Name, client.ServerAddr, client.ServerPort)
	return
}
Exemplo n.º 2
0
// if success, ret equals 0, otherwise greater than 0
func doLogin(req *msg.ControlReq, c *conn.Conn) (ret int64, info string) {
	ret = 1
	// check if proxy name exist
	s, ok := server.ProxyServers[req.ProxyName]
	if !ok {
		info = fmt.Sprintf("ProxyName [%s] is not exist", req.ProxyName)
		log.Warn(info)
		return
	}

	// check authKey
	nowTime := time.Now().Unix()
	authKey := pcrypto.GetAuthKey(req.ProxyName + s.AuthToken + fmt.Sprintf("%d", req.Timestamp))
	// authKey avaiable in 15 minutes
	if nowTime-req.Timestamp > 15*60 {
		info = fmt.Sprintf("ProxyName [%s], authorization timeout", req.ProxyName)
		log.Warn(info)
		return
	} else if req.AuthKey != authKey {
		info = fmt.Sprintf("ProxyName [%s], authorization failed", req.ProxyName)
		log.Warn(info)
		return
	}

	// control conn
	if req.Type == consts.NewCtlConn {
		if s.Status == consts.Working {
			info = fmt.Sprintf("ProxyName [%s], already in use", req.ProxyName)
			log.Warn(info)
			return
		}

		// set infomations from frpc
		s.UseEncryption = req.UseEncryption

		// start proxy and listen for user connections, no block
		err := s.Start(c)
		if err != nil {
			info = fmt.Sprintf("ProxyName [%s], start proxy error: %v", req.ProxyName, err)
			log.Warn(info)
			return
		}
		log.Info("ProxyName [%s], start proxy success", req.ProxyName)
	} else if req.Type == consts.NewWorkConn {
		// work conn
		if s.Status != consts.Working {
			log.Warn("ProxyName [%s], is not working when it gets one new work connnection", req.ProxyName)
			return
		}
		// the connection will close after join over
		s.RegisterNewWorkConn(c)
	} else {
		info = fmt.Sprintf("Unsupport login message type [%d]", req.Type)
		log.Warn("Unsupport login message type [%d]", req.Type)
		return
	}

	ret = 0
	return
}
Exemplo n.º 3
0
func (p *ProxyClient) GetRemoteConn(addr string, port int64) (c *conn.Conn, err error) {
	defer func() {
		if err != nil {
			c.Close()
		}
	}()

	c, err = conn.ConnectServer(addr, port)
	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))
	req := &msg.ControlReq{
		Type:      consts.NewWorkConn,
		ProxyName: p.Name,
		AuthKey:   authKey,
		Timestamp: nowTime,
	}

	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
}
Exemplo n.º 4
0
// if success, ret equals 0, otherwise greater than 0
func doLogin(req *msg.ControlReq, c *conn.Conn) (ret int64, info string) {
	ret = 1
	if req.PrivilegeMode && !server.PrivilegeMode {
		info = fmt.Sprintf("ProxyName [%s], PrivilegeMode is disabled in frps", req.ProxyName)
		log.Warn("info")
		return
	}

	var (
		s  *server.ProxyServer
		ok bool
	)
	s, ok = server.ProxyServers[req.ProxyName]
	if req.PrivilegeMode && req.Type == consts.NewCtlConn {
		log.Debug("ProxyName [%s], doLogin and privilege mode is enabled", req.ProxyName)
	} else {
		if !ok {
			info = fmt.Sprintf("ProxyName [%s] is not exist", req.ProxyName)
			log.Warn(info)
			return
		}
	}

	// check authKey or privilegeKey
	nowTime := time.Now().Unix()
	if req.PrivilegeMode {
		privilegeKey := pcrypto.GetAuthKey(req.ProxyName + server.PrivilegeToken + fmt.Sprintf("%d", req.Timestamp))
		// privilegeKey avaiable in 15 minutes
		if nowTime-req.Timestamp > 15*60 {
			info = fmt.Sprintf("ProxyName [%s], privilege mode authorization timeout", req.ProxyName)
			log.Warn(info)
			return
		} else if req.PrivilegeKey != privilegeKey {
			info = fmt.Sprintf("ProxyName [%s], privilege mode authorization failed", req.ProxyName)
			log.Warn(info)
			return
		}
	} else {
		authKey := pcrypto.GetAuthKey(req.ProxyName + s.AuthToken + fmt.Sprintf("%d", req.Timestamp))
		// authKey avaiable in 15 minutes
		if nowTime-req.Timestamp > 15*60 {
			info = fmt.Sprintf("ProxyName [%s], authorization timeout", req.ProxyName)
			log.Warn(info)
			return
		} else if req.AuthKey != authKey {
			info = fmt.Sprintf("ProxyName [%s], authorization failed", req.ProxyName)
			log.Warn(info)
			return
		}
	}

	// control conn
	if req.Type == consts.NewCtlConn {
		if req.PrivilegeMode {
			s = server.NewProxyServerFromCtlMsg(req)
			err := server.CreateProxy(s)
			if err != nil {
				info = fmt.Sprintf("ProxyName [%s], %v", req.ProxyName, err)
				log.Warn(info)
				return
			}
		}

		if s.Status == consts.Working {
			info = fmt.Sprintf("ProxyName [%s], already in use", req.ProxyName)
			log.Warn(info)
			return
		}

		// check if vhost_port is set
		if s.Type == "http" && server.VhostHttpMuxer == nil {
			info = fmt.Sprintf("ProxyName [%s], type [http] not support when vhost_http_port is not set", req.ProxyName)
			log.Warn(info)
			return
		}
		if s.Type == "https" && server.VhostHttpsMuxer == nil {
			info = fmt.Sprintf("ProxyName [%s], type [https] not support when vhost_https_port is not set", req.ProxyName)
			log.Warn(info)
			return
		}

		// set infomations from frpc
		s.UseEncryption = req.UseEncryption
		s.UseGzip = req.UseGzip

		// start proxy and listen for user connections, no block
		err := s.Start(c)
		if err != nil {
			info = fmt.Sprintf("ProxyName [%s], start proxy error: %v", req.ProxyName, err)
			log.Warn(info)
			return
		}
		log.Info("ProxyName [%s], start proxy success", req.ProxyName)
		if req.PrivilegeMode {
			log.Info("ProxyName [%s], created by PrivilegeMode", req.ProxyName)
		}
	} else if req.Type == consts.NewWorkConn {
		// work conn
		if s.Status != consts.Working {
			log.Warn("ProxyName [%s], is not working when it gets one new work connnection", req.ProxyName)
			return
		}
		// the connection will close after join over
		s.RegisterNewWorkConn(c)
	} else {
		info = fmt.Sprintf("Unsupport login message type [%d]", req.Type)
		log.Warn("Unsupport login message type [%d]", req.Type)
		return
	}

	ret = 0
	return
}