Compare commits
No commits in common. "main" and "default" have entirely different histories.
3
.github/workflows/test.yml
vendored
3
.github/workflows/test.yml
vendored
@ -19,9 +19,6 @@ jobs:
|
|||||||
- name: Setup Go Environment
|
- name: Setup Go Environment
|
||||||
uses: actions/setup-go@master
|
uses: actions/setup-go@master
|
||||||
|
|
||||||
- name: Run Go Test
|
|
||||||
run: go test $(go list ./...)
|
|
||||||
|
|
||||||
- name: Test Build
|
- name: Test Build
|
||||||
env:
|
env:
|
||||||
NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
|
NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
|
||||||
|
4
Makefile
4
Makefile
@ -21,7 +21,7 @@ TEST_EXPECTED := '{"code":200,'
|
|||||||
|
|
||||||
all:
|
all:
|
||||||
@BUILD_PATH=$(BUILD_PATH)/aarch64 TARGET_ARCH=aarch64 GOARCH=arm64 $(MAKE) -e shared
|
@BUILD_PATH=$(BUILD_PATH)/aarch64 TARGET_ARCH=aarch64 GOARCH=arm64 $(MAKE) -e shared
|
||||||
@BUILD_PATH=$(BUILD_PATH)/armv7a TARGET_ARCH=armv7a GOARCH=arm GOARM=7 TARGET_SDK=androideabi23 $(MAKE) -e shared
|
@BUILD_PATH=$(BUILD_PATH)/armv7a TARGET_ARCH=armv7a GOARCH=arm TARGET_SDK=androideabi23 $(MAKE) -e shared
|
||||||
@BUILD_PATH=$(BUILD_PATH)/i686 TARGET_ARCH=i686 GOARCH=amd64 $(MAKE) -e shared
|
@BUILD_PATH=$(BUILD_PATH)/i686 TARGET_ARCH=i686 GOARCH=amd64 $(MAKE) -e shared
|
||||||
@BUILD_PATH=$(BUILD_PATH)/x86_64 TARGET_ARCH=x86_64 GOARCH=386 $(MAKE) -e shared
|
@BUILD_PATH=$(BUILD_PATH)/x86_64 TARGET_ARCH=x86_64 GOARCH=386 $(MAKE) -e shared
|
||||||
rm -rf $(BUILD_PATH)/*/*.h
|
rm -rf $(BUILD_PATH)/*/*.h
|
||||||
@ -29,7 +29,7 @@ all:
|
|||||||
find $(BUILD_PATH) -mindepth 1 -maxdepth 1 -type d -exec mv {}/lib$(PROJECT_NAME).so.gz {}_lib$(PROJECT_NAME).so.gz \;; \
|
find $(BUILD_PATH) -mindepth 1 -maxdepth 1 -type d -exec mv {}/lib$(PROJECT_NAME).so.gz {}_lib$(PROJECT_NAME).so.gz \;; \
|
||||||
cd $(BUILD_PATH) && zip -r -9 $(BUILD_PATH).zip aarch64 armv7a i686 x86_64
|
cd $(BUILD_PATH) && zip -r -9 $(BUILD_PATH).zip aarch64 armv7a i686 x86_64
|
||||||
shared: $(GO_SRC) dir tidy
|
shared: $(GO_SRC) dir tidy
|
||||||
GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) NDK_TOOLCHAIN=$(NDK_TOOLCHAIN) CC=$(CC) go build -buildmode=c-shared -ldflags "-s -w" -trimpath -o $(BUILD_PATH)/lib$(PROJECT_NAME).so $(GO_SRC)
|
GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) NDK_TOOLCHAIN=$(NDK_TOOLCHAIN) CC=$(CC) go build -buildmode=c-shared -o $(BUILD_PATH)/lib$(PROJECT_NAME).so $(GO_SRC)
|
||||||
test: dir
|
test: dir
|
||||||
@GOOS=$(BUILD_MACHINE) CC=cc NDK_TOOLCHAIN="" $(MAKE) -e shared
|
@GOOS=$(BUILD_MACHINE) CC=cc NDK_TOOLCHAIN="" $(MAKE) -e shared
|
||||||
cc -o $(BUILD_PATH)/test $(BUILD_PATH)/test.c -l$(PROJECT_NAME) -L$(BUILD_PATH)
|
cc -o $(BUILD_PATH)/test $(BUILD_PATH)/test.c -l$(PROJECT_NAME) -L$(BUILD_PATH)
|
||||||
|
167
dns.go
167
dns.go
@ -1,30 +1,145 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/fumiama/terasu/dns"
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
|
||||||
func init() {
|
"github.com/fumiama/terasu"
|
||||||
dns.IPv4Servers.Add(&dns.DNSConfig{
|
)
|
||||||
Servers: map[string][]string{
|
|
||||||
"dot.360.cn": {
|
type dnsstat struct {
|
||||||
"101.198.192.33:853",
|
A string
|
||||||
"112.65.69.15:853",
|
E bool
|
||||||
"101.226.4.6:853",
|
}
|
||||||
"218.30.118.6:853",
|
|
||||||
"123.125.81.6:853",
|
type dnsservers struct {
|
||||||
"140.207.198.6:853",
|
sync.RWMutex
|
||||||
},
|
m map[string][]*dnsstat
|
||||||
},
|
}
|
||||||
})
|
|
||||||
dns.IPv6Servers.Add(&dns.DNSConfig{
|
// hasrecord no lock, use under lock
|
||||||
Servers: map[string][]string{
|
func hasrecord(lst []*dnsstat, a string) bool {
|
||||||
"dot.360.cn": {
|
for _, addr := range lst {
|
||||||
"101.198.192.33:853",
|
if addr.A == a {
|
||||||
"112.65.69.15:853",
|
return true
|
||||||
"101.226.4.6:853",
|
}
|
||||||
"218.30.118.6:853",
|
}
|
||||||
"123.125.81.6:853",
|
return false
|
||||||
"140.207.198.6:853",
|
}
|
||||||
},
|
|
||||||
},
|
func (ds *dnsservers) add(m map[string][]string) {
|
||||||
})
|
ds.Lock()
|
||||||
|
defer ds.Unlock()
|
||||||
|
addList := map[string][]*dnsstat{}
|
||||||
|
for host, addrs := range m {
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if !hasrecord(ds.m[host], addr) && !hasrecord(addList[host], addr) {
|
||||||
|
addList[host] = append(addList[host], &dnsstat{addr, true})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for host, addrs := range addList {
|
||||||
|
ds.m[host] = append(ds.m[host], addrs...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ds *dnsservers) dial(ctx context.Context) (tlsConn *tls.Conn, err error) {
|
||||||
|
ds.RLock()
|
||||||
|
defer ds.RUnlock()
|
||||||
|
|
||||||
|
if dialer.Timeout != 0 {
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, dialer.Timeout)
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dialer.Deadline.IsZero() {
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
ctx, cancel = context.WithDeadline(ctx, dialer.Deadline)
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
var conn net.Conn
|
||||||
|
for host, addrs := range ds.m {
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if !addr.E {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
conn, err = dialer.DialContext(ctx, "tcp", addr.A)
|
||||||
|
if err != nil {
|
||||||
|
addr.E = false // no need to acquire write lock
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tlsConn = tls.Client(conn, &tls.Config{ServerName: host})
|
||||||
|
err = terasu.Use(tlsConn).HandshakeContext(ctx)
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = tlsConn.Close()
|
||||||
|
addr.E = false // no need to acquire write lock
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var dotv6servers = dnsservers{
|
||||||
|
m: map[string][]*dnsstat{
|
||||||
|
"dot.sb": {
|
||||||
|
{"[2a09::]:853", true},
|
||||||
|
{"[2a11::]:853", true},
|
||||||
|
},
|
||||||
|
"dns.google": {
|
||||||
|
{"[2001:4860:4860::8888]:853", true},
|
||||||
|
{"[2001:4860:4860::8844]:853", true},
|
||||||
|
},
|
||||||
|
"cloudflare-dns.com": {
|
||||||
|
{"[2606:4700:4700::1111]:853", true},
|
||||||
|
{"[2606:4700:4700::1001]:853", true},
|
||||||
|
},
|
||||||
|
"dns.umbrella.com": {
|
||||||
|
{"[2620:0:ccc::2]:853", true},
|
||||||
|
{"[2620:0:ccd::2]:853", true},
|
||||||
|
},
|
||||||
|
"dns10.quad9.net": {
|
||||||
|
{"[2620:fe::10]:853", true},
|
||||||
|
{"[2620:fe::fe:10]:853", true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var dotv4servers = dnsservers{
|
||||||
|
m: map[string][]*dnsstat{
|
||||||
|
"dot.sb": {
|
||||||
|
{"185.222.222.222:853", true},
|
||||||
|
{"45.11.45.11:853", true},
|
||||||
|
},
|
||||||
|
"dns.google": {
|
||||||
|
{"8.8.8.8:853", true},
|
||||||
|
{"8.8.4.4:853", true},
|
||||||
|
},
|
||||||
|
"cloudflare-dns.com": {
|
||||||
|
{"1.1.1.1:853", true},
|
||||||
|
{"1.0.0.1:853", true},
|
||||||
|
},
|
||||||
|
"dns.umbrella.com": {
|
||||||
|
{"208.67.222.222:853", true},
|
||||||
|
{"208.67.220.220:853", true},
|
||||||
|
},
|
||||||
|
"dns10.quad9.net": {
|
||||||
|
{"9.9.9.10:853", true},
|
||||||
|
{"149.112.112.10:853", true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var resolver = &net.Resolver{
|
||||||
|
PreferGo: true,
|
||||||
|
Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||||
|
if canUseIPv6.Get() {
|
||||||
|
return dotv6servers.dial(ctx)
|
||||||
|
}
|
||||||
|
return dotv4servers.dial(ctx)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
52
dns_test.go
Normal file
52
dns_test.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/fumiama/terasu"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolver(t *testing.T) {
|
||||||
|
t.Log("canUseIPv6:", canUseIPv6.Get())
|
||||||
|
addrs, err := resolver.LookupHost(context.TODO(), "dns.google")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(addrs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNS(t *testing.T) {
|
||||||
|
if canUseIPv6.Get() {
|
||||||
|
dotv6servers.test()
|
||||||
|
}
|
||||||
|
dotv4servers.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ds *dnsservers) test() {
|
||||||
|
ds.RLock()
|
||||||
|
defer ds.RUnlock()
|
||||||
|
for host, addrs := range ds.m {
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if !addr.E {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fmt.Println("dial:", host, addr.A)
|
||||||
|
conn, err := net.Dial("tcp", addr.A)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tlsConn := tls.Client(conn, &tls.Config{ServerName: host})
|
||||||
|
err = terasu.Use(tlsConn).Handshake()
|
||||||
|
_ = tlsConn.Close()
|
||||||
|
if err == nil {
|
||||||
|
fmt.Println("succ:", host, addr.A)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fmt.Println("fail:", host, addr.A)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
go.mod
12
go.mod
@ -2,11 +2,11 @@ module comandy
|
|||||||
|
|
||||||
go 1.22.1
|
go 1.22.1
|
||||||
|
|
||||||
require github.com/fumiama/terasu v0.0.0-20240502091919-c887e26289a8
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1 // indirect
|
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1
|
||||||
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 // indirect
|
github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e
|
||||||
golang.org/x/net v0.24.0 // indirect
|
github.com/fumiama/terasu v0.0.0-20240416061047-62d3c9f6be80
|
||||||
golang.org/x/text v0.14.0 // indirect
|
golang.org/x/net v0.24.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require golang.org/x/text v0.14.0 // indirect
|
||||||
|
12
go.sum
12
go.sum
@ -1,15 +1,11 @@
|
|||||||
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1 h1:g4pTnDJUW4VbJ9NvoRfUvdjDrHz/6QhfN/LoIIpICbo=
|
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1 h1:g4pTnDJUW4VbJ9NvoRfUvdjDrHz/6QhfN/LoIIpICbo=
|
||||||
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
|
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
|
||||||
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 h1:S/ferNiehVjNaBMNNBxUjLtVmP/YWD6Yh79RfPv4ehU=
|
github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e h1:wR3MXQ3VbUlPKOOUwLOYgh/QaJThBTYtsl673O3lqSA=
|
||||||
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=
|
github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/fumiama/terasu v0.0.0-20240418160715-5999fcf7db84 h1:dQ4GUL1OHOHHlZ0SOi9ltsuqnmwCUeMNbaLQYs8U1N0=
|
github.com/fumiama/terasu v0.0.0-20240416061047-62d3c9f6be80 h1:O1JJZzcd5ggUw/9X8V9KxBZ9JZGWFmX/r1q2TPg+pZQ=
|
||||||
github.com/fumiama/terasu v0.0.0-20240418160715-5999fcf7db84/go.mod h1:afchyfKAb7J/zvaENtYzjIEPVbwiEjJaow05zzT4usM=
|
github.com/fumiama/terasu v0.0.0-20240416061047-62d3c9f6be80/go.mod h1:BFl0X1+rGJf8bLHl/kO+v05ryHrj/R4kyCrK89NvegA=
|
||||||
github.com/fumiama/terasu v0.0.0-20240418161858-1c3273a78268 h1:6R8kSGVSIoR3xm2NG8Z4ivkTpAET783RFsJOVKrI7n8=
|
|
||||||
github.com/fumiama/terasu v0.0.0-20240418161858-1c3273a78268/go.mod h1:afchyfKAb7J/zvaENtYzjIEPVbwiEjJaow05zzT4usM=
|
|
||||||
github.com/fumiama/terasu v0.0.0-20240502091919-c887e26289a8 h1:mVOgOhlrF0ra8/BkwVA71ev/1HkzAgDn8gWU2UNbDU8=
|
|
||||||
github.com/fumiama/terasu v0.0.0-20240502091919-c887e26289a8/go.mod h1:UVx8YP1jKKL1Cj+uy+OnQRM2Ih6U36Mqy9GSf7jabsI=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||||
|
82
http.go
82
http.go
@ -1,17 +1,86 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fumiama/terasu/http2"
|
"github.com/FloatTech/ttl"
|
||||||
|
"github.com/fumiama/terasu"
|
||||||
|
"golang.org/x/net/http2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var dialer = net.Dialer{
|
||||||
|
Timeout: time.Minute,
|
||||||
|
}
|
||||||
|
|
||||||
|
var lookupTable = ttl.NewCache[string, []string](time.Hour)
|
||||||
|
|
||||||
|
type comandyClient http.Client
|
||||||
|
|
||||||
|
var cli = comandyClient(http.Client{
|
||||||
|
Transport: &http2.Transport{
|
||||||
|
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
|
||||||
|
if dialer.Timeout != 0 {
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, dialer.Timeout)
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dialer.Deadline.IsZero() {
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
ctx, cancel = context.WithDeadline(ctx, dialer.Deadline)
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
host, port, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
addrs := lookupTable.Get(host)
|
||||||
|
if len(addrs) == 0 {
|
||||||
|
addrs, err = resolver.LookupHost(ctx, host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
lookupTable.Set(host, addrs)
|
||||||
|
}
|
||||||
|
if len(addr) == 0 {
|
||||||
|
return nil, errors.New("empty host addr")
|
||||||
|
}
|
||||||
|
var tlsConn *tls.Conn
|
||||||
|
for _, a := range addrs {
|
||||||
|
if strings.Contains(a, ":") {
|
||||||
|
a = "[" + a + "]:" + port
|
||||||
|
} else {
|
||||||
|
a += ":" + port
|
||||||
|
}
|
||||||
|
conn, err := dialer.DialContext(ctx, network, a)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tlsConn = tls.Client(conn, cfg)
|
||||||
|
err = terasu.Use(tlsConn).HandshakeContext(ctx)
|
||||||
|
if err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
_ = tlsConn.Close()
|
||||||
|
}
|
||||||
|
return tlsConn, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
type capsule struct {
|
type capsule struct {
|
||||||
C int `json:"code,omitempty"`
|
C int `json:"code,omitempty"`
|
||||||
M string `json:"method,omitempty"`
|
M string `json:"method,omitempty"`
|
||||||
@ -36,7 +105,7 @@ func (r *capsule) printstrerr(err string) string {
|
|||||||
return buf.String()
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func gorequest(para string) (ret string) {
|
func (cli *comandyClient) request(para string) (ret string) {
|
||||||
r := capsule{}
|
r := capsule{}
|
||||||
defer func() {
|
defer func() {
|
||||||
err := recover()
|
err := recover()
|
||||||
@ -44,6 +113,12 @@ func gorequest(para string) (ret string) {
|
|||||||
ret = r.printstrerr(fmt.Sprint())
|
ret = r.printstrerr(fmt.Sprint())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
_ = canUseIPv6.Get()
|
||||||
|
}()
|
||||||
err := json.Unmarshal(stringToBytes(para), &r)
|
err := json.Unmarshal(stringToBytes(para), &r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return r.printerr(err)
|
return r.printerr(err)
|
||||||
@ -78,7 +153,8 @@ func gorequest(para string) (ret string) {
|
|||||||
return r.printstrerr("unsupported H type " + reflect.ValueOf(x).Type().Name())
|
return r.printstrerr("unsupported H type " + reflect.ValueOf(x).Type().Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resp, err := http2.DefaultClient.Do(req)
|
wg.Wait()
|
||||||
|
resp, err := (*http.Client)(cli).Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return r.printerr(err)
|
return r.printerr(err)
|
||||||
}
|
}
|
||||||
|
69
http_test.go
69
http_test.go
@ -1,49 +1,41 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
|
||||||
"testing"
|
"testing"
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"github.com/fumiama/terasu/dns"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type testlist struct {
|
func TestClientGet(t *testing.T) {
|
||||||
sync.RWMutex
|
_ = canUseIPv6.Get()
|
||||||
m map[string][]*uintptr
|
req, err := http.NewRequest("GET", "https://api.mangacopy.com/api/v3/h5/homeIndex?platform=3", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
resp, err := (*http.Client)(&cli).Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
t.Log("[T] response code", resp.StatusCode)
|
||||||
|
for k, vs := range resp.Header {
|
||||||
|
for _, v := range vs {
|
||||||
|
t.Log("[T] response header", k+":", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(data) == 0 {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
t.Log(bytesToString(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRequest(t *testing.T) {
|
func TestRequest(t *testing.T) {
|
||||||
(*testlist)(unsafe.Pointer(&dns.IPv4Servers)).m = make(map[string][]*uintptr)
|
r := cli.request(`{"code":0,"headers":{"authorization":"Token ","host":"api.mangacopy.com","source":"copyApp","webp":"1","region":"1","version":"2.1.7","platform":"3","user-agent":"COPY/2.1.7"},"method":"GET","url":"https://api.mangacopy.com/api/v3/h5/homeIndex?platform\u003d3"}`)
|
||||||
(*testlist)(unsafe.Pointer(&dns.IPv6Servers)).m = make(map[string][]*uintptr)
|
|
||||||
dns.IPv4Servers.Add(&dns.DNSConfig{
|
|
||||||
Servers: map[string][]string{
|
|
||||||
"dot.360.cn": {
|
|
||||||
"101.198.192.33:853",
|
|
||||||
"112.65.69.15:853",
|
|
||||||
"101.226.4.6:853",
|
|
||||||
"218.30.118.6:853",
|
|
||||||
"123.125.81.6:853",
|
|
||||||
"140.207.198.6:853",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
dns.IPv6Servers.Add(&dns.DNSConfig{
|
|
||||||
Servers: map[string][]string{
|
|
||||||
"dot.360.cn": {
|
|
||||||
"101.198.192.33:853",
|
|
||||||
"112.65.69.15:853",
|
|
||||||
"101.226.4.6:853",
|
|
||||||
"218.30.118.6:853",
|
|
||||||
"123.125.81.6:853",
|
|
||||||
"140.207.198.6:853",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
r := gorequest(`{"code":0,"headers":{"authorization":"Token ","host":"api.mangacopy.com","source":"copyApp","webp":"1","region":"1","version":"2.1.7","platform":"3","user-agent":"COPY/2.1.7"},"method":"GET","url":"https://api.mangacopy.com/api/v3/h5/homeIndex?platform\u003d3"}`)
|
|
||||||
t.Log(r)
|
t.Log(r)
|
||||||
c := capsule{}
|
c := capsule{}
|
||||||
err := json.Unmarshal(stringToBytes(r), &c)
|
err := json.Unmarshal(stringToBytes(r), &c)
|
||||||
@ -51,12 +43,7 @@ func TestRequest(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if c.C != http.StatusOK {
|
if c.C != http.StatusOK {
|
||||||
s, err := base64.StdEncoding.DecodeString(c.D)
|
t.Fatal("status code", c.C)
|
||||||
if err != nil {
|
|
||||||
t.Fatal("status code", c.C, "msg:", c.D)
|
|
||||||
} else {
|
|
||||||
t.Fatal("status code", c.C, "msg:", s)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if len(c.D) == 0 {
|
if len(c.D) == 0 {
|
||||||
t.Fatal("empty data")
|
t.Fatal("empty data")
|
||||||
|
16
ipv6.go
Normal file
16
ipv6.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/RomiChan/syncx"
|
||||||
|
)
|
||||||
|
|
||||||
|
var canUseIPv6 = syncx.Lazy[bool]{Init: func() bool {
|
||||||
|
resp, err := http.Get("http://v6.ipv6-test.com/json/widgetdata.php?callback=?")
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_ = resp.Body.Close()
|
||||||
|
return true
|
||||||
|
}}
|
11
main.go
11
main.go
@ -4,9 +4,6 @@ import "C"
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/fumiama/terasu/dns"
|
|
||||||
"github.com/fumiama/terasu/ip"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {}
|
func main() {}
|
||||||
@ -21,13 +18,13 @@ func add_dns(para *C.char, is_ipv6 C.int) *C.char {
|
|||||||
return C.CString(err.Error())
|
return C.CString(err.Error())
|
||||||
}
|
}
|
||||||
if is_ipv6 != 0 {
|
if is_ipv6 != 0 {
|
||||||
if !ip.IsIPv6Available.Get() {
|
if !canUseIPv6.Get() {
|
||||||
return C.CString("cannot use ipv6")
|
return C.CString("cannot use ipv6")
|
||||||
}
|
}
|
||||||
dns.IPv6Servers.Add(&dns.DNSConfig{Servers: m})
|
dotv6servers.add(m)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
dns.IPv4Servers.Add(&dns.DNSConfig{Servers: m})
|
dotv4servers.add(m)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,5 +40,5 @@ func add_dns(para *C.char, is_ipv6 C.int) *C.char {
|
|||||||
//
|
//
|
||||||
//export request
|
//export request
|
||||||
func request(para *C.char) *C.char {
|
func request(para *C.char) *C.char {
|
||||||
return C.CString(gorequest(C.GoString(para)))
|
return C.CString(cli.request(C.GoString(para)))
|
||||||
}
|
}
|
||||||
|
2
utils.go
2
utils.go
@ -14,12 +14,10 @@ type slice struct {
|
|||||||
cap int
|
cap int
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
// bytesToString 没有内存开销的转换
|
// bytesToString 没有内存开销的转换
|
||||||
func bytesToString(b []byte) string {
|
func bytesToString(b []byte) string {
|
||||||
return *(*string)(unsafe.Pointer(&b))
|
return *(*string)(unsafe.Pointer(&b))
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
// stringToBytes 没有内存开销的转换
|
// stringToBytes 没有内存开销的转换
|
||||||
func stringToBytes(s string) (b []byte) {
|
func stringToBytes(s string) (b []byte) {
|
||||||
|
Loading…
Reference in New Issue
Block a user