优化代码

This commit is contained in:
kony
2025-09-22 16:25:08 +08:00
parent b21b9d8828
commit 15ff28faa5
5 changed files with 14 additions and 76 deletions

View File

@@ -141,9 +141,19 @@ func RedisDel() {
m_redis_db.Del(m_md5_tun_key)
}
func GetUDPLocalIPPort(level string) (string, int) {
conn, err := net.Dial(level, "ifconfig.co:80")
if err != nil {
return "", 0
}
defer conn.Close()
return conn.LocalAddr().(*net.UDPAddr).IP.String(), conn.LocalAddr().(*net.UDPAddr).Port
}
func GetUDPAddr() (conn *net.UDPConn, addr tun.AddrType) {
addr.LocalIPv4, _ = utils.GetUDPLocalIPPort("udp4")
addr.IPv6, _ = utils.GetUDPLocalIPPort("udp6")
addr.LocalIPv4, _ = GetUDPLocalIPPort("udp4")
addr.IPv6, _ = GetUDPLocalIPPort("udp6")
var err error

View File

@@ -23,7 +23,7 @@ func GetLocalQuicConn(conn *net.UDPConn, addr *tun.AddrType, count int) (*tun.Tu
var tun_active *tun.TunActive
var tun_passive *tun.TunPassive
SessionID := string(utils.RandomBytes(24))
SessionID := string(go2.RandomBytes(24))
utils.Log().DebugF("会话ID: %s", SessionID)
redisJson := RedisJsonType{

View File

@@ -10,7 +10,6 @@ import (
"goodlink/config"
"goodlink/pro"
_ "goodlink/pro"
"goodlink/utils"
_ "embed"
_ "net/http/pprof"
@@ -47,7 +46,7 @@ func GetMainUI(myWindow *fyne.Window) *fyne.Container {
}
m_button_key_create = widget.NewButton("生成密钥", func() {
m_validated_key.SetText(string(utils.RandomBytes(24)))
m_validated_key.SetText(string(go2.RandomBytes(24)))
})
key_copy_button := widget.NewButton("复制密钥", func() {
clipboard.WriteAll(m_validated_key.Text)

View File

@@ -1,51 +0,0 @@
package utils
import (
"crypto/tls"
"io"
"net"
"net/http"
"strconv"
"strings"
"time"
)
func GetUDPLocalIPPort(level string) (string, int) {
conn, err := net.Dial(level, "ifconfig.co:80")
if err != nil {
return "", 0
}
defer conn.Close()
return conn.LocalAddr().(*net.UDPAddr).IP.String(), conn.LocalAddr().(*net.UDPAddr).Port
}
func GetTCPWanIPv4Port(dialer *net.Dialer) (string, int) {
var res []byte
var err error
var resp *http.Response
client := &http.Client{
Transport: &http.Transport{
DialContext: dialer.DialContext,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // 跳过证书验证
},
},
Timeout: 3 * time.Second,
}
if resp, err = client.Get("https://goodlink.kony.vip/ip"); err != nil {
return "", 0
}
defer resp.Body.Close()
if res, err = io.ReadAll(resp.Body); err != nil {
return "", 0
}
localAddr := string(res)
ip := strings.Split(localAddr, ":")[0]
port, _ := strconv.Atoi(strings.Split(localAddr, ":")[1])
return ip, port
}

View File

@@ -1,20 +0,0 @@
package utils
import (
"crypto/rand"
"math/big"
)
func RandomBytes(length int) []byte {
bytes := make([]byte, length)
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for i := 0; i < length; {
bint, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
return nil
}
bytes[i] = charset[bint.Int64()]
i++
}
return bytes
}