2025-05-13

This commit is contained in:
Qin tian
2025-05-13 09:38:07 +08:00
parent d05b42b097
commit 4547cfd2a8
6 changed files with 45 additions and 23 deletions

View File

@@ -57,7 +57,7 @@ func clean() {
time.Sleep(time.Minute) time.Sleep(time.Minute)
whoisLock.Lock() whoisLock.Lock()
for key, v := range httpTypeMap { for key, v := range httpTypeMap {
if time.Now().Sub(v._time) > time.Minute*10 { if time.Now().Sub(v._time) > time.Minute*9 {
delete(httpTypeMap, key) delete(httpTypeMap, key)
} }
} }
@@ -254,6 +254,23 @@ func createLocalCert(Sunny *Sunny, cert *x509.Certificate, serverName, host stri
var mHost string var mHost string
var keyName string var keyName string
var err error var err error
if cert != nil {
not := time.Now().AddDate(0, 0, 365)
certByte, priByte, er := generatePem(cert, mHost, parent, priv)
if er == nil {
certificate, er1 := tls.X509KeyPair(certByte, priByte)
if er1 == nil {
DNSNames := cert.DNSNames
for _, v := range cert.IPAddresses {
DNSNames = append(DNSNames, v.String())
}
whoisLock.Lock()
whois[host] = &_cert{Cert: &certificate, Type: netCert, Expire: &not, DNSNames: DNSNames}
whoisLock.Unlock()
return &certificate, DNSNames
}
}
}
if serverName == "" || serverName == "null" { if serverName == "" || serverName == "null" {
//是否为DNS解析服务器,如果是直接本地生成证书即可,就不需要从网络获取证书了 //是否为DNS解析服务器,如果是直接本地生成证书即可,就不需要从网络获取证书了
if !strings.HasSuffix(host, ":853") { if !strings.HasSuffix(host, ":853") {
@@ -308,7 +325,8 @@ func createNetCert(Sunny *Sunny, cert *x509.Certificate, host string, parent *x5
if rr == nil { if rr == nil {
return nil, nil, _GetIpCertError return nil, nil, _GetIpCertError
} }
certByte, priByte, not, er := generatePem(rr, mHost, parent, priv) not := time.Now().AddDate(0, 0, 365)
certByte, priByte, er := generatePem(rr, mHost, parent, priv)
if er != nil { if er != nil {
return nil, nil, er return nil, nil, er
} }
@@ -321,7 +339,7 @@ func createNetCert(Sunny *Sunny, cert *x509.Certificate, host string, parent *x5
DNSNames = append(DNSNames, v.String()) DNSNames = append(DNSNames, v.String())
} }
whoisLock.Lock() whoisLock.Lock()
whois[host] = &_cert{Cert: &certificate, Type: netCert, Expire: not, DNSNames: DNSNames} whois[host] = &_cert{Cert: &certificate, Type: netCert, Expire: &not, DNSNames: DNSNames}
whoisLock.Unlock() whoisLock.Unlock()
return &certificate, DNSNames, nil return &certificate, DNSNames, nil
} }
@@ -394,7 +412,7 @@ func GetIpAddressHost(proxy *SunnyProxy.Proxy, ipAddress string, outRouterIP *ne
var _GetIpCertError = fmt.Errorf("no success Get Certificate") var _GetIpCertError = fmt.Errorf("no success Get Certificate")
func generatePem(template *x509.Certificate, mHost string, parent *x509.Certificate, priv *rsa.PrivateKey) ([]byte, []byte, *time.Time, error) { func generatePem(template *x509.Certificate, mHost string, parent *x509.Certificate, priv *rsa.PrivateKey) ([]byte, []byte, error) {
template1 := x509.Certificate{ template1 := x509.Certificate{
SerialNumber: template.SerialNumber, // 序列号CA 颁发的唯一序列号,通常为随机生成 SerialNumber: template.SerialNumber, // 序列号CA 颁发的唯一序列号,通常为随机生成
Subject: template.Subject, // 证书主题,包含持有者的信息(国家、组织等) Subject: template.Subject, // 证书主题,包含持有者的信息(国家、组织等)
@@ -426,7 +444,7 @@ func generatePem(template *x509.Certificate, mHost string, parent *x509.Certific
} }
cer, err := x509.CreateCertificate(rand.Reader, &template1, parent, &priv.PublicKey, priv) cer, err := x509.CreateCertificate(rand.Reader, &template1, parent, &priv.PublicKey, priv)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, err
} }
return pem.EncodeToMemory(&pem.Block{ // 证书 return pem.EncodeToMemory(&pem.Block{ // 证书
Type: "CERTIFICATE", Type: "CERTIFICATE",
@@ -434,7 +452,7 @@ func generatePem(template *x509.Certificate, mHost string, parent *x509.Certific
}), pem.EncodeToMemory(&pem.Block{ // 私钥 }), pem.EncodeToMemory(&pem.Block{ // 私钥
Type: "RSA PRIVATE KEY", Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priv), Bytes: x509.MarshalPKCS1PrivateKey(priv),
}), &template1.NotAfter, err }), err
} }
func generatePemTemp(mHost string, parent *x509.Certificate, priv *rsa.PrivateKey) ([]byte, []byte, *time.Time, error) { func generatePemTemp(mHost string, parent *x509.Certificate, priv *rsa.PrivateKey) ([]byte, []byte, *time.Time, error) {
serialNumber, _ := rand.Int(rand.Reader, public.MaxBig) serialNumber, _ := rand.Int(rand.Reader, public.MaxBig)
@@ -511,10 +529,12 @@ func init() {
rootKey = obj.rootKey rootKey = obj.rootKey
tempNetLock.Unlock() tempNetLock.Unlock()
rr, err = GetIpAddressHost(obj.proxy, host, obj.outRouterIP) rr, err = GetIpAddressHost(obj.proxy, host, obj.outRouterIP)
not1 := time.Now().AddDate(0, 0, 365)
not = &not1
if rr == nil { if rr == nil {
goto gg goto gg
} }
certByte, priByte, not, err = generatePem(rr, host, rootCa, rootKey) certByte, priByte, err = generatePem(rr, host, rootCa, rootKey)
if err != nil { if err != nil {
goto gg goto gg
} }

View File

@@ -823,6 +823,7 @@ func (s *proxyRequest) httpProcessing(aheadData []byte, Tag string) {
h2, _ = s.RwObj.Peek(11 - len(aheadData)) h2, _ = s.RwObj.Peek(11 - len(aheadData))
} }
hh = []byte(string(aheadData) + string(h2)) hh = []byte(string(aheadData) + string(h2))
if string(hh) == "PRI * HTTP/" { if string(hh) == "PRI * HTTP/" {
s.defaultScheme = "https" s.defaultScheme = "https"
s.h2Request(aheadData) s.h2Request(aheadData)
@@ -1160,7 +1161,11 @@ func (s *proxyRequest) https() {
err = noHttps err = noHttps
} else { } else {
tlsConfig.Certificates = []tls.Certificate{*certificate} tlsConfig.Certificates = []tls.Certificate{*certificate}
tlsConfig.ServerName = ServerName if serverName != "" {
tlsConfig.ServerName = serverName
} else {
tlsConfig.ServerName = ServerName
}
tlsConfig.InsecureSkipVerify = true tlsConfig.InsecureSkipVerify = true
//tlsConfig.CipherSuites= //tlsConfig.CipherSuites=
//继续握手 //继续握手
@@ -1183,6 +1188,7 @@ func (s *proxyRequest) https() {
} }
_ = tlsConn.SetDeadline(time.Now().Add(30 * time.Second)) _ = tlsConn.SetDeadline(time.Now().Add(30 * time.Second))
} }
} }
} else { } else {
isRules := s.Global.tcpRules(serverName, s.Target.Host) isRules := s.Global.tcpRules(serverName, s.Target.Host)

12
main.go
View File

@@ -1,17 +1,5 @@
package main package main
import "C"
import (
"github.com/qtgolang/SunnyNet/src/http"
_ "github.com/qtgolang/SunnyNet/src/http/pprof"
)
func init() {
go func() {
_ = http.ListenAndServe("0.0.0.0:6001", nil)
}()
}
func main() { func main() {
Test() Test()
} }

View File

@@ -13,6 +13,8 @@ import (
"github.com/qtgolang/SunnyNet/src/GoScriptCode/yaegi/stdlib" "github.com/qtgolang/SunnyNet/src/GoScriptCode/yaegi/stdlib"
"github.com/qtgolang/SunnyNet/src/Interface" "github.com/qtgolang/SunnyNet/src/Interface"
"github.com/qtgolang/SunnyNet/src/RSA" "github.com/qtgolang/SunnyNet/src/RSA"
"github.com/qtgolang/SunnyNet/src/http"
_ "github.com/qtgolang/SunnyNet/src/http/pprof"
"github.com/qtgolang/SunnyNet/src/protobuf" "github.com/qtgolang/SunnyNet/src/protobuf"
"github.com/qtgolang/SunnyNet/src/public" "github.com/qtgolang/SunnyNet/src/public"
"reflect" "reflect"
@@ -20,6 +22,12 @@ import (
"strings" "strings"
) )
func init() {
go func() {
_ = http.ListenAndServe("0.0.0.0:6001", nil)
}()
}
type GoScriptTypeHTTP func(Interface.ConnHTTPScriptCall) type GoScriptTypeHTTP func(Interface.ConnHTTPScriptCall)
type GoScriptTypeWS func(Interface.ConnWebSocketScriptCall) type GoScriptTypeWS func(Interface.ConnWebSocketScriptCall)
type GoScriptTypeTCP func(Interface.ConnTCPScriptCall) type GoScriptTypeTCP func(Interface.ConnTCPScriptCall)

View File

@@ -794,9 +794,9 @@ window.builtCmdWords = [
name: ['BytesToString', '到字符串', '字节集到字符串'], name: ['BytesToString', '到字符串', '字节集到字符串'],
zName: [["dzfc","到字符串"],["zjjdzfc","字节集到字符串"]], zName: [["dzfc","到字符串"],["zjjdzfc","字节集到字符串"]],
insertText: 'BytesToString(${1:bs}$0)', insertText: 'BytesToString(${1:bs}$0)',
detail: "字符串到字节集", detail: "字节集到字符串",
contents: [ contents: [
{value: '**字符串到字节集**'}, {value: '**字节集到字符串**'},
{value: '**参数说明**'}, {value: '**参数说明**'},
{value: '参数1:bs 字节集类型 [待转换的字节集]'}, {value: '参数1:bs 字节集类型 [待转换的字节集]'},
{value: '返回值:字符串'}, {value: '返回值:字符串'},

View File

@@ -16,7 +16,7 @@ import (
"time" "time"
) )
const SunnyVersion = "2025-05-06" const SunnyVersion = "2025-05-13"
const Information = ` const Information = `
------------------------------------------------------ ------------------------------------------------------
欢迎使用 SunnyNet 网络中间件 - V` + SunnyVersion + ` 欢迎使用 SunnyNet 网络中间件 - V` + SunnyVersion + `