Commit 9e515716 authored by wei.xuan's avatar wei.xuan

fix: fix dns lookup on android

parent c85f5dba
package main
import (
"context"
"flag"
"log"
"math/rand"
"net"
"net/http"
_ "net/http/pprof" //nolint:gosec
"runtime"
......@@ -36,6 +38,14 @@ func init() {
flag.StringVar(&account, "account", "unknown", "account")
flag.Parse()
// for android
net.DefaultResolver = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
return net.Dial("udp", common.DNSServer)
},
}
}
func initPprof() {
......
......@@ -74,7 +74,7 @@ func (client *Client) WriteAndFlush(packet []byte) error {
}
func (client *Client) handleTransfer(packet *protocol.MajoraPacket) {
conn, ok := client.GetConnection(packet)
conn, ok := client.GetConnection(packet, "handleTransfer")
// 如何把这个错误告诉服务端
if !ok {
return
......@@ -82,7 +82,7 @@ func (client *Client) handleTransfer(packet *protocol.MajoraPacket) {
if cnt, err := conn.Write(packet.Data); err != nil {
logger.Warn().Msgf("write with error cnt=%d|err=%+v", cnt, err)
client.RemoveConnection(packet, "write_error")
client.removeConnection(packet, "write_error")
}
}
......@@ -94,18 +94,18 @@ func (client *Client) handleConnection(conn net.Conn, packet *protocol.MajoraPac
if !errors.Is(err, net.ErrClosed) && !errors.Is(err, io.EOF) {
logger.Error().Msgf("handleConnection peek with error:%+v", err)
}
client.connStore.Delete(packet.SerialNumber)
client.removeConnection(packet, "peek_error")
break
}
bufsize := reader.Buffered()
logger.Debug().Msgf("bufsize %d", bufsize)
buf := make([]byte, bufsize)
_, err := reader.Read(buf)
if err != nil {
logger.Error().Msgf("handleConnection read with error:%+v", err)
break
}
logger.Debug().Msgf("handleConnection %s", string(buf))
pack := protocol.TypeTransfer.CreatePacket()
pack.Data = buf
pack.SerialNumber = packet.SerialNumber
......@@ -117,11 +117,11 @@ func (client *Client) handleConnection(conn net.Conn, packet *protocol.MajoraPac
}
func (client *Client) handleDisconnectMessage(packet *protocol.MajoraPacket) {
client.RemoveConnection(packet, "from_server")
client.removeConnection(packet, "from_server")
}
func (client *Client) handleControlMessage(packet *protocol.MajoraPacket) {
logger.Debug().Msgf("handleControlMessage %s", string(packet.Data))
func (client *Client) handleControlMessage(_ *protocol.MajoraPacket) {
logger.Debug().Msg("handleControlMessage ")
}
func (client *Client) handleDestroyMessage() {
......@@ -142,15 +142,15 @@ func (client *Client) AddConnection(packet *protocol.MajoraPacket, conn net.Conn
logger.Debug().Msgf("create connection for %d", packet.SerialNumber)
}
// RemoveConnection 1. 本地缓存删除 2. 关闭连接 3. 通知natserver
func (client *Client) RemoveConnection(packet *protocol.MajoraPacket, reason string) {
// removeConnection 1. 本地缓存删除 2. 关闭连接 3. 通知natserver
func (client *Client) removeConnection(packet *protocol.MajoraPacket, reason string) {
defer func() {
if err := recover(); err != nil {
logger.Error().Msgf("RemoveConnection panic %+v", err)
logger.Error().Msgf("removeConnection panic %+v", err)
}
}()
// delete from local cache
conn, ok := client.GetConnection(packet)
conn, ok := client.GetConnection(packet, "removeConnection")
if ok {
client.connStore.Delete(packet.SerialNumber)
// 直接关闭是否就可以 主动断开 是否有剩余数据已经意义不大了
......@@ -167,13 +167,13 @@ func (client *Client) RemoveConnection(packet *protocol.MajoraPacket, reason str
}
}
func (client *Client) GetConnection(packet *protocol.MajoraPacket) (conn net.Conn, ok bool) {
func (client *Client) GetConnection(packet *protocol.MajoraPacket, step string) (conn net.Conn, ok bool) {
load, ok := client.connStore.Load(packet.SerialNumber)
// 没有的话 可能是服务端未感知到端上的连接已断开了
// 是否需要主动创建一个
if !ok || load == nil {
logger.Warn().Msgf("can not find connection for %d", packet.SerialNumber)
logger.Warn().Msgf("can not find connection for %s->%d", step, packet.SerialNumber)
return nil, false
}
conn, ok = load.(net.Conn)
......
package common
const (
DNSServer = "114.114.114.114:53"
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment