mirror of
https://gitee.com/willfree/tcpserver-client.git
synced 2026-06-16 17:18:07 +08:00
地址服务器存储一个地址改为存储多个地址
This commit is contained in:
+34
-23
@@ -7,6 +7,7 @@ import (
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -25,7 +26,7 @@ type AddrServer struct {
|
||||
requestInfo string
|
||||
syncInfo string
|
||||
password string
|
||||
addr CanBeUsedAddr // 可用服务器的地址
|
||||
addrs []CanBeUsedAddr // 可用服务器的地址
|
||||
}
|
||||
|
||||
// 开启server,并建立监听
|
||||
@@ -38,12 +39,17 @@ func (s *AddrServer) start() {
|
||||
s.requestInfo=DefaultRequestInfo
|
||||
s.password=DefaultPassword
|
||||
s.syncInfo=DefaultSyncInfo
|
||||
|
||||
// 创建addrs清理协程
|
||||
go s.verifyIPAddrList()
|
||||
|
||||
// 创建 listener
|
||||
listener, err := net.Listen("tcp", s.serverIP+":"+strconv.Itoa(s.serverPort))
|
||||
if err != nil {
|
||||
fmt.Println("Error listening", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 监听并接受来自客户端或服务器的连接,并进行处理
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
@@ -78,9 +84,9 @@ func (s *AddrServer) doMainServerStuff(conn net.Conn) {
|
||||
fmt.Println("Received decrypt data: %v", msg)
|
||||
|
||||
// 判断是何种请求
|
||||
if(msg == s.authInfo){
|
||||
s.doRequestStuff(conn)
|
||||
}else if(msg == s.syncInfo){
|
||||
if strings.HasPrefix(msg,s.authInfo) {
|
||||
s.doRequestStuff(conn,msg)
|
||||
}else if msg == s.syncInfo {
|
||||
s.doSyncStuff(conn)
|
||||
}else{
|
||||
fmt.Println("Unable to handle request: %v", msg)
|
||||
@@ -94,22 +100,12 @@ func (s *AddrServer) doMainServerStuff(conn net.Conn) {
|
||||
/**
|
||||
处理客户端请求:只对请求可用服务器地址的客户端连接进行响应
|
||||
*/
|
||||
func (s *AddrServer) doRequestStuff(conn net.Conn) {
|
||||
func (s *AddrServer) doRequestStuff(conn net.Conn,msg string) {
|
||||
// 向Server验证用户身份
|
||||
// 读取原始数据
|
||||
buf := make([]byte, 512)
|
||||
len, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading", err.Error())
|
||||
return
|
||||
}
|
||||
msg:=string(buf[:len])
|
||||
fmt.Println("Received raw data: %v", msg)
|
||||
// 解密数据为可读格式
|
||||
msg=utils.DecryptMsgByDES(msg, s.password)
|
||||
fmt.Println("Received decrypt data: %v", msg)
|
||||
// 读取用户证书
|
||||
auth:=msg[len(s.authInfo):]
|
||||
// 向Server发送用户证书,验证用户身份; 验证通过,向客户端返回"true"
|
||||
b,err:=s.userAuthentication()
|
||||
b,err:=s.userAuthentication(auth)
|
||||
if !b {
|
||||
return
|
||||
}else{
|
||||
@@ -117,7 +113,7 @@ func (s *AddrServer) doRequestStuff(conn net.Conn) {
|
||||
}
|
||||
|
||||
// 将该子网地址返回给客户端
|
||||
_ , err = conn.Write([]byte(s.addr.ip))
|
||||
_ , err = conn.Write([]byte(s.addrs[0].ip))
|
||||
if err != nil {
|
||||
fmt.Println("Error writing", err.Error())
|
||||
}else{
|
||||
@@ -132,13 +128,28 @@ func (s *AddrServer) doRequestStuff(conn net.Conn) {
|
||||
func (s *AddrServer) doSyncStuff(conn net.Conn) {
|
||||
// 取出conn地址,更新到s.addr中
|
||||
addrS := conn.RemoteAddr().String()
|
||||
s.addr.ip=strings.Split(addrS,":")[0]
|
||||
fmt.Println("Updated addr success : "+addrS)
|
||||
cb:=CanBeUsedAddr{}
|
||||
cb.ip=strings.Split(addrS,":")[0]
|
||||
s.addrs = append(s.addrs, cb)
|
||||
fmt.Println("Added addr success : "+addrS)
|
||||
_ = conn.Close()
|
||||
}
|
||||
|
||||
// 验证用户身份
|
||||
func (s *AddrServer) userAuthentication() (bool,error){
|
||||
/**
|
||||
定期清理不可连接的IP地址
|
||||
*/
|
||||
func (s *AddrServer) verifyIPAddrList() {
|
||||
fmt.Println("start addr cleaner...")
|
||||
for {
|
||||
for i := 0; i < len(s.addrs); i++ {
|
||||
// todo:验证IP是否可用,不可用,则删掉
|
||||
}
|
||||
time.Sleep(time.Hour)
|
||||
}
|
||||
}
|
||||
|
||||
// todo:验证用户身份
|
||||
func (s *AddrServer) userAuthentication(auth string) (bool,error){
|
||||
// 与min server建立连接
|
||||
//...
|
||||
|
||||
|
||||
+6
-6
@@ -88,17 +88,17 @@ func (c *Client) getSubServerAddr(conn net.Conn) (string,error) {
|
||||
// 验证用户身份
|
||||
func (c *Client) userAuthentication(conn net.Conn) (bool,error){
|
||||
// 发送验证用户身份的请求给服务器
|
||||
_,err := conn.Write([]byte(utils.EncryptMsgByDES(c.authInfo,c.password)))
|
||||
_,err := conn.Write([]byte(utils.EncryptMsgByDES(c.authInfo+c.user.GetCertification(),c.password)))
|
||||
if err != nil {
|
||||
fmt.Println("Error writing", err.Error())
|
||||
return false,err
|
||||
}
|
||||
|
||||
_,err = conn.Write([]byte(utils.EncryptMsgByDES(c.user.GetCertification(),c.password)))
|
||||
if err != nil {
|
||||
fmt.Println("Error writing", err.Error())
|
||||
return false,err
|
||||
}
|
||||
//_,err = conn.Write([]byte(utils.EncryptMsgByDES(c.user.GetCertification(),c.password)))
|
||||
//if err != nil {
|
||||
// fmt.Println("Error writing", err.Error())
|
||||
// return false,err
|
||||
//}
|
||||
|
||||
// 接收验证结果
|
||||
buf := make([]byte, 512)
|
||||
|
||||
Reference in New Issue
Block a user