mirror of
https://gitee.com/willfree/tcpserver-client.git
synced 2026-06-16 20:08:04 +08:00
184 lines
4.5 KiB
Go
184 lines
4.5 KiB
Go
package main
|
|
// 提供可用服务器地址的服务器
|
|
|
|
import (
|
|
"TCPServerClient/utils"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
DefaultServerIP = "localhost" // 本机服务器IP
|
|
DefaultServerPort = 50000 // 本机服务器端口号
|
|
DefaultRequestInfo = "Request Addr" // 请求可用服务器IP的命令字段
|
|
DefaultAuthenticationInfo = "User Authentication" // 验证用户身份的命令字段
|
|
DefaultSyncInfo = "Sync Addr" // 同步IP地址的命令字段
|
|
DefaultPassword = "12345678" // 消息加解密密码。必须是八位
|
|
)
|
|
|
|
type AddrServer struct {
|
|
serverIP string // 本机服务器IP
|
|
serverPort int // 本机服务器端口号
|
|
authInfo string
|
|
requestInfo string
|
|
syncInfo string
|
|
password string
|
|
addrs []CanBeUsedAddr // 可用服务器的地址
|
|
}
|
|
|
|
// 开启server,并建立监听
|
|
func (s *AddrServer) start() {
|
|
fmt.Println("Starting the addr server ...")
|
|
// 初始化
|
|
s.serverIP= DefaultServerIP
|
|
s.serverPort= DefaultServerPort
|
|
s.authInfo=DefaultAuthenticationInfo
|
|
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()
|
|
if err != nil {
|
|
fmt.Println("Error accepting", err.Error())
|
|
return
|
|
}else{
|
|
fmt.Println("Main AddrServer Accept conn form "+conn.RemoteAddr().String())
|
|
}
|
|
// 处理请求
|
|
go s.doMainServerStuff(conn)
|
|
}
|
|
}
|
|
|
|
/**
|
|
处理TCP请求
|
|
*/
|
|
func (s *AddrServer) doMainServerStuff(conn net.Conn) {
|
|
for {
|
|
// 读取原始数据
|
|
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)
|
|
|
|
// 判断是何种请求
|
|
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)
|
|
continue
|
|
}
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
/**
|
|
处理客户端请求:只对请求可用服务器地址的客户端连接进行响应
|
|
*/
|
|
func (s *AddrServer) doRequestStuff(conn net.Conn,msg string) {
|
|
// 向Server验证用户身份
|
|
// 读取用户证书
|
|
auth:=msg[len(s.authInfo):]
|
|
// 向Server发送用户证书,验证用户身份; 验证通过,向客户端返回"true"
|
|
b,err:=s.userAuthentication(auth)
|
|
if !b {
|
|
return
|
|
}else{
|
|
_ , err = conn.Write([]byte("true"))
|
|
}
|
|
|
|
// 将该子网地址返回给客户端
|
|
_ , err = conn.Write([]byte(s.addrs[0].ip))
|
|
if err != nil {
|
|
fmt.Println("Error writing", err.Error())
|
|
}else{
|
|
fmt.Println("Send addr information to client successfully.")
|
|
}
|
|
_ = conn.Close()
|
|
}
|
|
|
|
/**
|
|
处理服务器请求:同步服务器地址
|
|
*/
|
|
func (s *AddrServer) doSyncStuff(conn net.Conn) {
|
|
// 取出conn地址,更新到s.addr中
|
|
addrS := conn.RemoteAddr().String()
|
|
cb:=CanBeUsedAddr{}
|
|
cb.ip=strings.Split(addrS,":")[0]
|
|
s.addrs = append(s.addrs, cb)
|
|
fmt.Println("Added addr success : "+addrS)
|
|
_ = conn.Close()
|
|
}
|
|
|
|
/**
|
|
定期清理不可连接的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建立连接
|
|
//...
|
|
|
|
//// 发送验证用户身份的请求给服务器
|
|
//_,err := conn.Write([]byte(utils.EncryptMsgByDES(c.authInfo,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)
|
|
//len,err := conn.Read(buf)
|
|
//if err!=nil {
|
|
// fmt.Println("Error reading", err.Error())
|
|
// return false,err
|
|
//}
|
|
//if string(buf[:len])=="true" {
|
|
// fmt.Println("authentication success")
|
|
// return true,nil
|
|
//}else{
|
|
// return false,errors.New("authentication failed")
|
|
//}
|
|
fmt.Println("authentication success")
|
|
return true,nil
|
|
} |