Skip to content

Commit

Permalink
Split Service interface into outline-ss-server and Caddy interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed Jan 8, 2025
1 parent 23a03e9 commit aa130f0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
14 changes: 9 additions & 5 deletions caddy/shadowsocks_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package caddy

import (
"container/list"
"context"
"fmt"
"log/slog"
"net"
Expand All @@ -32,6 +33,11 @@ const serverUDPBufferSize = 64 * 1024

const ssModuleName = "layer4.handlers.shadowsocks"

type OutlineService interface {
HandleStream(ctx context.Context, conn transport.StreamConn)
HandleAssociation(conn net.Conn) error
}

func init() {
caddy.RegisterModule(ModuleRegistration{
ID: ssModuleName,
Expand All @@ -48,7 +54,7 @@ type KeyConfig struct {
type ShadowsocksHandler struct {
Keys []KeyConfig `json:"keys,omitempty"`

service outline.Service
service OutlineService
logger *slog.Logger
}

Expand Down Expand Up @@ -119,11 +125,9 @@ func (h *ShadowsocksHandler) Handle(cx *layer4.Connection, _ layer4.Handler) err
case transport.StreamConn:
h.service.HandleStream(cx.Context, conn)
case net.Conn:
assoc, err := h.service.NewPacketAssociation(conn)
if err != nil {
return fmt.Errorf("failed to handle association: %v", err)
if err := h.service.HandleAssociation(conn); err != nil {
return err
}
outline.HandleAssociation(conn, assoc)
default:
return fmt.Errorf("failed to handle unknown connection type: %t", conn)
}
Expand Down
11 changes: 10 additions & 1 deletion cmd/outline-ss-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"container/list"
"context"
"flag"
"fmt"
"log/slog"
Expand All @@ -27,6 +28,7 @@ import (
"syscall"
"time"

"github.com/Jigsaw-Code/outline-sdk/transport"
"github.com/Jigsaw-Code/outline-sdk/transport/shadowsocks"
"github.com/lmittmann/tint"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -60,6 +62,11 @@ func init() {
)
}

type OutlineService interface {
HandleStream(ctx context.Context, conn transport.StreamConn)
NewPacketAssociation(conn net.Conn) (service.PacketAssociation, error)
}

type OutlineServer struct {
stopConfig func() error
lnManager service.ListenerManager
Expand Down Expand Up @@ -223,6 +230,7 @@ func (s *OutlineServer) runConfig(config Config) (func() error, error) {
ciphers := service.NewCipherList()
ciphers.Update(cipherList)

var ssService OutlineService
ssService, err := service.NewShadowsocksService(
service.WithCiphers(ciphers),
service.WithMetrics(s.serviceMetrics),
Expand Down Expand Up @@ -250,7 +258,8 @@ func (s *OutlineServer) runConfig(config Config) (func() error, error) {
if err != nil {
return fmt.Errorf("failed to create cipher list from config: %v", err)
}
ssService, err := service.NewShadowsocksService(
var ssService OutlineService
ssService, err = service.NewShadowsocksService(
service.WithCiphers(ciphers),
service.WithMetrics(s.serviceMetrics),
service.WithReplayCache(&s.replayCache),
Expand Down
19 changes: 13 additions & 6 deletions service/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package service

import (
"context"
"fmt"
"log/slog"
"net"
"time"
Expand All @@ -41,11 +42,6 @@ type ServiceMetrics interface {
AddCipherSearch(proto string, accessKeyFound bool, timeToCipher time.Duration)
}

type Service interface {
HandleStream(ctx context.Context, conn transport.StreamConn)
NewPacketAssociation(conn net.Conn) (PacketAssociation, error)
}

// Option is a Shadowsocks service constructor option.
type Option func(s *ssService)

Expand All @@ -63,7 +59,7 @@ type ssService struct {
}

// NewShadowsocksService creates a new Shadowsocks service.
func NewShadowsocksService(opts ...Option) (Service, error) {
func NewShadowsocksService(opts ...Option) (*ssService, error) {
s := &ssService{}

for _, opt := range opts {
Expand Down Expand Up @@ -146,6 +142,17 @@ func (s *ssService) HandleStream(ctx context.Context, conn transport.StreamConn)
s.sh.Handle(ctx, conn, metrics)
}

// HandleAssociation handles a Shadowsocks packet-based connection.
func (s *ssService) HandleAssociation(conn net.Conn) error {
assoc, err := s.NewPacketAssociation(conn)
if err != nil {
return fmt.Errorf("failed to handle association: %v", err)
}
HandleAssociation(conn, assoc)
return nil
}


// NewPacketAssociation creates a new Shadowsocks packet-based association.
func (s *ssService) NewPacketAssociation(conn net.Conn) (PacketAssociation, error) {
var metrics UDPAssociationMetrics
Expand Down

0 comments on commit aa130f0

Please sign in to comment.