Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions x/soax/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type ProxySessionConfig struct {

// ProxySession represents a session with unique SessionID, created from a SessionConfig.
type ProxySession struct {
config *ProxySessionConfig
config ProxySessionConfig
}

func (c *ProxySessionConfig) newUserPassword() *url.Userinfo {
Expand Down Expand Up @@ -109,8 +109,7 @@ func (c *ProxySessionConfig) newUserPassword() *url.Userinfo {

func (c *ProxySessionConfig) NewSession() *ProxySession {
session := new(ProxySession)
// Copy the config to not modify the original one.
session.config = c
session.config = *c
if session.config.Session != SessionNotPersistent {
if session.config.Session.ID == "" {
session.config.Session.ID = strconv.Itoa(int(time.Now().UnixMilli()))
Expand Down Expand Up @@ -145,10 +144,10 @@ func (c *ProxySession) NewSOCKS5Client() (*socks5.Client, error) {
return client, nil
}

// NewStreamDialer creates a [transport.StreamDialer] that connects through the SOAX proxy.
// NewWebProxyStreamDialer creates a [transport.StreamDialer] that connects through the SOAX proxy.
// It uses HTTP CONNECT, so it only supports TCP.
func (c *ProxySession) NewWebProxyStreamDialer() (transport.StreamDialer, error) {
rt, err := httpconnect.NewHTTPProxyTransport(&transport.TCPDialer{}, c.config.Endpoint)
func (c *ProxySession) NewWebProxyStreamDialer(opts ...httpconnect.TransportOption) (transport.StreamDialer, error) {
rt, err := httpconnect.NewHTTPProxyTransport(&transport.TCPDialer{}, c.config.Endpoint, opts...)
if err != nil {
return nil, err
}
Expand Down
59 changes: 58 additions & 1 deletion x/soax/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is an "AS IS" BASIS,
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Expand All @@ -16,15 +16,19 @@ package soax

import (
"context"
"encoding/base64"
"errors"
"net"
"net/http"
"net/http/httptest"
"net/url"
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/things-go/go-socks5"
"golang.getoutline.org/sdk/x/httpconnect"
)

func TestSessionConfig_newUsername_AllFields(t *testing.T) {
Expand Down Expand Up @@ -164,3 +168,56 @@ func TestSession_NewSOCKS5Client(t *testing.T) {
_, err = client.DialStream(context.Background(), targetListener.Addr().String())
require.NoError(t, err)
}

func TestSession_NewWebProxyStreamDialer(t *testing.T) {
config := ProxySessionConfig{
Auth: ProxyAuthConfig{
PackageID: 123456,
PackageKey: "my_package_key",
},
}

var mu sync.Mutex
var capturedAuth string

proxySrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodConnect, r.Method)
mu.Lock()
capturedAuth = r.Header.Get("Proxy-Authorization")
mu.Unlock()

hijacker := w.(http.Hijacker)
conn, buf, err := hijacker.Hijack()
require.NoError(t, err)
defer conn.Close()

_, err = buf.WriteString("HTTP/1.1 200 Connection established\r\n\r\n")
require.NoError(t, err)
require.NoError(t, buf.Flush())
}))
defer proxySrv.Close()

proxyURL, err := url.Parse(proxySrv.URL)
require.NoError(t, err)
config.Endpoint = proxyURL.Host
session := config.NewSession()
expectedUserinfo := session.config.newUserPassword()

dialer, err := session.NewWebProxyStreamDialer(httpconnect.WithPlainHTTP())
require.NoError(t, err)
require.NotNil(t, dialer)

// We need a listener so the proxy has a target to report connecting to.
targetListener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer targetListener.Close()

_, err = dialer.DialStream(context.Background(), targetListener.Addr().String())
require.NoError(t, err)

mu.Lock()
actualAuth := capturedAuth
mu.Unlock()
expectedAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(expectedUserinfo.String()))
require.Equal(t, expectedAuth, actualAuth)
}