feat(osi): 实现 OSI JSON 传输层(T-003)
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
package osi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
type TransportConfig struct {
|
||||
Timeout time.Duration
|
||||
Socks5Proxy string
|
||||
}
|
||||
|
||||
type Transport struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewTransport(config TransportConfig) (*Transport, error) {
|
||||
timeout := config.Timeout
|
||||
if timeout <= 0 {
|
||||
timeout = 20 * time.Second
|
||||
}
|
||||
|
||||
roundTripper := http.DefaultTransport.(*http.Transport).Clone()
|
||||
if strings.TrimSpace(config.Socks5Proxy) != "" {
|
||||
dialer, err := socks5Dialer(config.Socks5Proxy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
roundTripper.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
if contextDialer, ok := dialer.(proxy.ContextDialer); ok {
|
||||
return contextDialer.DialContext(ctx, network, address)
|
||||
}
|
||||
return dialer.Dial(network, address)
|
||||
}
|
||||
}
|
||||
|
||||
return &Transport{client: &http.Client{Timeout: timeout, Transport: roundTripper}}, nil
|
||||
}
|
||||
|
||||
func (t *Transport) PostJSON(ctx context.Context, targetURL string, payload any, headers map[string]string) (int, []byte, error) {
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return 0, nil, fmt.Errorf("marshal json payload: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, targetURL, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return 0, nil, fmt.Errorf("build request: %w", err)
|
||||
}
|
||||
for name, value := range headers {
|
||||
req.Header.Set(name, value)
|
||||
}
|
||||
if req.Header.Get("Content-Type") == "" {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
resp, err := t.client.Do(req)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
raw, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return resp.StatusCode, nil, fmt.Errorf("read response body: %w", err)
|
||||
}
|
||||
return resp.StatusCode, raw, nil
|
||||
}
|
||||
|
||||
func socks5Dialer(rawProxy string) (proxy.Dialer, error) {
|
||||
proxyURL, err := normalizeSocks5Proxy(rawProxy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dialer, err := proxy.SOCKS5("tcp", proxyURL.Host, nil, proxy.Direct)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create socks5 dialer: %w", err)
|
||||
}
|
||||
return dialer, nil
|
||||
}
|
||||
|
||||
func normalizeSocks5Proxy(rawProxy string) (*url.URL, error) {
|
||||
rawProxy = strings.TrimSpace(rawProxy)
|
||||
if !strings.Contains(rawProxy, "://") {
|
||||
rawProxy = "socks5://" + rawProxy
|
||||
}
|
||||
|
||||
proxyURL, err := url.Parse(rawProxy)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse socks5 proxy: %w", err)
|
||||
}
|
||||
if proxyURL.Scheme != "socks5" && proxyURL.Scheme != "socks5h" {
|
||||
return nil, fmt.Errorf("unsupported proxy scheme %q", proxyURL.Scheme)
|
||||
}
|
||||
if proxyURL.User != nil {
|
||||
return nil, fmt.Errorf("socks5 proxy authentication is not supported yet")
|
||||
}
|
||||
if _, _, err := net.SplitHostPort(proxyURL.Host); err != nil {
|
||||
return nil, fmt.Errorf("invalid socks5 proxy host %q: %w", proxyURL.Host, err)
|
||||
}
|
||||
return proxyURL, nil
|
||||
}
|
||||
Reference in New Issue
Block a user