|
|
|
|
@ -7,11 +7,13 @@ import (
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
"crypto/x509"
|
|
|
|
|
"encoding/binary"
|
|
|
|
|
"encoding/pem"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
@ -101,6 +103,14 @@ func TlsConnect(host string, port, timeout int, certBytes, keyBytes []byte) (con
|
|
|
|
|
return *tls.Client(_conn, conf), err
|
|
|
|
|
}
|
|
|
|
|
func getRequestTlsConfig(certBytes, keyBytes []byte) (conf *tls.Config, err error) {
|
|
|
|
|
block, _ := pem.Decode(certBytes)
|
|
|
|
|
if block == nil {
|
|
|
|
|
panic("failed to parse certificate PEM")
|
|
|
|
|
}
|
|
|
|
|
x509Cert, _ := x509.ParseCertificate(block.Bytes)
|
|
|
|
|
if x509Cert == nil {
|
|
|
|
|
panic("failed to parse block")
|
|
|
|
|
}
|
|
|
|
|
var cert tls.Certificate
|
|
|
|
|
cert, err = tls.X509KeyPair(certBytes, keyBytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
@ -114,8 +124,21 @@ func getRequestTlsConfig(certBytes, keyBytes []byte) (conf *tls.Config, err erro
|
|
|
|
|
conf = &tls.Config{
|
|
|
|
|
RootCAs: serverCertPool,
|
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
|
ServerName: "proxy",
|
|
|
|
|
InsecureSkipVerify: false,
|
|
|
|
|
ServerName: x509Cert.Subject.CommonName,
|
|
|
|
|
// VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
|
|
|
|
|
// opts := x509.VerifyOptions{
|
|
|
|
|
// Roots: serverCertPool,
|
|
|
|
|
// }
|
|
|
|
|
// for _, rawCert := range rawCerts {
|
|
|
|
|
// cert, _ := x509.ParseCertificate(rawCert)
|
|
|
|
|
// _, err := cert.Verify(opts)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return err
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return nil
|
|
|
|
|
// },
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
@ -136,6 +159,14 @@ func ConnectKCPHost(hostAndPort, method, key string) (conn net.Conn, err error)
|
|
|
|
|
return kcpconn, err
|
|
|
|
|
}
|
|
|
|
|
func ListenTls(ip string, port int, certBytes, keyBytes []byte) (ln *net.Listener, err error) {
|
|
|
|
|
block, _ := pem.Decode(certBytes)
|
|
|
|
|
if block == nil {
|
|
|
|
|
panic("failed to parse certificate PEM")
|
|
|
|
|
}
|
|
|
|
|
x509Cert, _ := x509.ParseCertificate(block.Bytes)
|
|
|
|
|
if x509Cert == nil {
|
|
|
|
|
panic("failed to parse block")
|
|
|
|
|
}
|
|
|
|
|
var cert tls.Certificate
|
|
|
|
|
cert, err = tls.X509KeyPair(certBytes, keyBytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
@ -148,9 +179,23 @@ func ListenTls(ip string, port int, certBytes, keyBytes []byte) (ln *net.Listene
|
|
|
|
|
}
|
|
|
|
|
config := &tls.Config{
|
|
|
|
|
ClientCAs: clientCertPool,
|
|
|
|
|
ServerName: "proxy",
|
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
|
|
|
ServerName: x509Cert.Subject.CommonName,
|
|
|
|
|
// VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
|
|
|
|
|
// opts := x509.VerifyOptions{
|
|
|
|
|
// Roots: clientCertPool,
|
|
|
|
|
// }
|
|
|
|
|
// for _, rawCert := range rawCerts {
|
|
|
|
|
// cert, _ := x509.ParseCertificate(rawCert)
|
|
|
|
|
// _, err := cert.Verify(opts)
|
|
|
|
|
// fmt.Println("SERVER ERR:", err)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return err
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return nil
|
|
|
|
|
// },
|
|
|
|
|
}
|
|
|
|
|
_ln, err := tls.Listen("tcp", fmt.Sprintf("%s:%d", ip, port), config)
|
|
|
|
|
if err == nil {
|
|
|
|
|
@ -200,7 +245,14 @@ func Keygen() (err error) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(string(out))
|
|
|
|
|
cmd = exec.Command("sh", "-c", `openssl req -new -key proxy.key -x509 -days 3650 -out proxy.crt -subj /C=CN/ST=BJ/O="Localhost Ltd"/CN=proxy`)
|
|
|
|
|
CList := []string{"AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AR", "AT", "AU", "AZ", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BR", "BS", "BW", "BY", "BZ", "CA", "CF", "CG", "CH", "CK", "CL", "CM", "CN", "CO", "CR", "CS", "CU", "CY", "CZ", "DE", "DJ", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "ET", "FI", "FJ", "FR", "GA", "GB", "GD", "GE", "GF", "GH", "GI", "GM", "GN", "GR", "GT", "GU", "GY", "HK", "HN", "HT", "HU", "ID", "IE", "IL", "IN", "IQ", "IR", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KP", "KR", "KT", "KW", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "MG", "ML", "MM", "MN", "MO", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NE", "NG", "NI", "NL", "NO", "NP", "NR", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PR", "PT", "PY", "QA", "RO", "RU", "SA", "SB", "SC", "SD", "SE", "SG", "SI", "SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV", "SY", "SZ", "TD", "TG", "TH", "TJ", "TM", "TN", "TO", "TR", "TT", "TW", "TZ", "UA", "UG", "US", "UY", "UZ", "VC", "VE", "VN", "YE", "YU", "ZA", "ZM", "ZR", "ZW"}
|
|
|
|
|
domainSubfixList := []string{".com", ".edu", ".gov", ".int", ".mil", ".net", ".org", ".biz", ".info", ".pro", ".name", ".museum", ".coop", ".aero", ".xxx", ".idv", ".ac", ".ad", ".ae", ".af", ".ag", ".ai", ".al", ".am", ".an", ".ao", ".aq", ".ar", ".as", ".at", ".au", ".aw", ".az", ".ba", ".bb", ".bd", ".be", ".bf", ".bg", ".bh", ".bi", ".bj", ".bm", ".bn", ".bo", ".br", ".bs", ".bt", ".bv", ".bw", ".by", ".bz", ".ca", ".cc", ".cd", ".cf", ".cg", ".ch", ".ci", ".ck", ".cl", ".cm", ".cn", ".co", ".cr", ".cu", ".cv", ".cx", ".cy", ".cz", ".de", ".dj", ".dk", ".dm", ".do", ".dz", ".ec", ".ee", ".eg", ".eh", ".er", ".es", ".et", ".eu", ".fi", ".fj", ".fk", ".fm", ".fo", ".fr", ".ga", ".gd", ".ge", ".gf", ".gg", ".gh", ".gi", ".gl", ".gm", ".gn", ".gp", ".gq", ".gr", ".gs", ".gt", ".gu", ".gw", ".gy", ".hk", ".hm", ".hn", ".hr", ".ht", ".hu", ".id", ".ie", ".il", ".im", ".in", ".io", ".iq", ".ir", ".is", ".it", ".je", ".jm", ".jo", ".jp", ".ke", ".kg", ".kh", ".ki", ".km", ".kn", ".kp", ".kr", ".kw", ".ky", ".kz", ".la", ".lb", ".lc", ".li", ".lk", ".lr", ".ls", ".lt", ".lu", ".lv", ".ly", ".ma", ".mc", ".md", ".mg", ".mh", ".mk", ".ml", ".mm", ".mn", ".mo", ".mp", ".mq", ".mr", ".ms", ".mt", ".mu", ".mv", ".mw", ".mx", ".my", ".mz", ".na", ".nc", ".ne", ".nf", ".ng", ".ni", ".nl", ".no", ".np", ".nr", ".nu", ".nz", ".om", ".pa", ".pe", ".pf", ".pg", ".ph", ".pk", ".pl", ".pm", ".pn", ".pr", ".ps", ".pt", ".pw", ".py", ".qa", ".re", ".ro", ".ru", ".rw", ".sa", ".sb", ".sc", ".sd", ".se", ".sg", ".sh", ".si", ".sj", ".sk", ".sl", ".sm", ".sn", ".so", ".sr", ".st", ".sv", ".sy", ".sz", ".tc", ".td", ".tf", ".tg", ".th", ".tj", ".tk", ".tl", ".tm", ".tn", ".to", ".tp", ".tr", ".tt", ".tv", ".tw", ".tz", ".ua", ".ug", ".uk", ".um", ".us", ".uy", ".uz", ".va", ".vc", ".ve", ".vg", ".vi", ".vn", ".vu", ".wf", ".ws", ".ye", ".yt", ".yu", ".yr", ".za", ".zm", ".zw"}
|
|
|
|
|
C := CList[int(RandInt(4))%len(CList)]
|
|
|
|
|
ST := RandString(int(RandInt(4) % 10))
|
|
|
|
|
O := RandString(int(RandInt(4) % 10))
|
|
|
|
|
CN := strings.ToLower(RandString(int(RandInt(4)%10)) + domainSubfixList[int(RandInt(4))%len(domainSubfixList)])
|
|
|
|
|
cmdStr := fmt.Sprintf("openssl req -new -key proxy.key -x509 -days 36500 -out proxy.crt -subj /C=%s/ST=%s/O=%s/CN=%s", C, ST, O, CN)
|
|
|
|
|
cmd = exec.Command("sh", "-c", cmdStr)
|
|
|
|
|
out, err = cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("err:%s", err)
|
|
|
|
|
@ -306,6 +358,29 @@ func Uniqueid() string {
|
|
|
|
|
// s := fmt.Sprintf("%d", src.Int63())
|
|
|
|
|
// return s[len(s)-5:len(s)-1] + fmt.Sprintf("%d", uint64(time.Now().UnixNano()))[8:]
|
|
|
|
|
}
|
|
|
|
|
func RandString(strlen int) string {
|
|
|
|
|
codes := "QWERTYUIOPLKJHGFDSAZXCVBNMabcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
|
codeLen := len(codes)
|
|
|
|
|
data := make([]byte, strlen)
|
|
|
|
|
rand.Seed(time.Now().UnixNano() + rand.Int63() + rand.Int63() + rand.Int63() + rand.Int63())
|
|
|
|
|
for i := 0; i < strlen; i++ {
|
|
|
|
|
idx := rand.Intn(codeLen)
|
|
|
|
|
data[i] = byte(codes[idx])
|
|
|
|
|
}
|
|
|
|
|
return string(data)
|
|
|
|
|
}
|
|
|
|
|
func RandInt(strLen int) int64 {
|
|
|
|
|
codes := "123456789"
|
|
|
|
|
codeLen := len(codes)
|
|
|
|
|
data := make([]byte, strLen)
|
|
|
|
|
rand.Seed(time.Now().UnixNano() + rand.Int63() + rand.Int63() + rand.Int63() + rand.Int63())
|
|
|
|
|
for i := 0; i < strLen; i++ {
|
|
|
|
|
idx := rand.Intn(codeLen)
|
|
|
|
|
data[i] = byte(codes[idx])
|
|
|
|
|
}
|
|
|
|
|
i, _ := strconv.ParseInt(string(data), 10, 64)
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
func ReadData(r io.Reader) (data string, err error) {
|
|
|
|
|
var len uint16
|
|
|
|
|
err = binary.Read(r, binary.LittleEndian, &len)
|
|
|
|
|
|