Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ssconf support #268

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
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
88 changes: 77 additions & 11 deletions x/examples/outline-cli/outline_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
package main

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"

Expand All @@ -42,7 +46,18 @@ type OutlineDevice struct {
var configToDialer = config.NewDefaultConfigToDialer()

func NewOutlineDevice(transportConfig string) (od *OutlineDevice, err error) {
ip, err := resolveShadowsocksServerIPFromConfig(transportConfig)
if err := validateConfig(transportConfig); err != nil {
return nil, err
}
parsed, err := url.Parse(transportConfig)
if err != nil {
return nil, fmt.Errorf("failed to parse config URL: %w", err)
}
transportConfig, err = formatConfig(parsed)
if err != nil {
return nil, err
}
ip, err := resolveShadowsocksServerIPFromHostname(parsed.Hostname())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -75,21 +90,72 @@ func (d *OutlineDevice) GetServerIP() net.IP {
return d.svrIP
}

func resolveShadowsocksServerIPFromConfig(transportConfig string) (net.IP, error) {
if strings.Contains(transportConfig, "|") {
return nil, errors.New("multi-part config is not supported")
// based on ssconf spec: https://reddit.com/r/outlinevpn/w/index/dynamic_access_keys
func constructShadowsocksSessionConfig(resp []byte) (string, error) {
var cfg struct {
Server string `json:"server"`
ServerPort string `json:"server_port"`
Password string `json:"password"`
Method string `json:"method"`
Prefix string `json:"prefix,omitempty"` // optional
}
if transportConfig = strings.TrimSpace(transportConfig); transportConfig == "" {
return nil, errors.New("config is required")

if err := json.Unmarshal([]byte(resp), &cfg); err != nil {
return "", fmt.Errorf("failed to parse response JSON: %w", err)
}
url, err := url.Parse(transportConfig)

// TODO: unsure what to do with prefix field
var cfgURL url.URL
cfgURL.Scheme = "ss"
cfgURL.User = url.User(base64.StdEncoding.EncodeToString([]byte(cfg.Method + ":" + cfg.Password)))
cfgURL.Host = cfg.Server + ":" + cfg.ServerPort

return cfgURL.String(), nil
}

func fetchShadowsocksSessionConfig(transportConfig string) ([]byte, error) {
transportConfig = "https" + strings.TrimPrefix(transportConfig, "ssconf")

resp, err := http.Get(transportConfig)
if err != nil {
return nil, fmt.Errorf("failed to parse config: %w", err)
return nil, fmt.Errorf("failed to fetch config from ssconf: %w", err)
}
if url.Scheme != "ss" {
return nil, errors.New("config must start with 'ss://'")
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to ready body from request: %w", err)
}
defer resp.Body.Close()

return body, nil
}

func formatConfig(transportConfigURL *url.URL) (string, error) {
switch transportConfigURL.Scheme {
case "ss":
return transportConfigURL.String(), nil
case "ssconf":
fetched, err := fetchShadowsocksSessionConfig(transportConfigURL.String())
if err != nil {
return "", err
}
return constructShadowsocksSessionConfig(fetched)
default:
return "", errors.New("config must start with 'ss://' or 'ssconf://'")
}
}

func validateConfig(transportConfig string) error {
if strings.Contains(transportConfig, "|") {
return errors.New("multi-part config is not supported")
}
ipList, err := net.LookupIP(url.Hostname())
if transportConfig = strings.TrimSpace(transportConfig); transportConfig == "" {
return errors.New("config is required")
}
return nil
}

func resolveShadowsocksServerIPFromHostname(hostname string) (net.IP, error) {
ipList, err := net.LookupIP(hostname)
if err != nil {
return nil, fmt.Errorf("invalid server hostname: %w", err)
}
Expand Down