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

proxy: move proxyprotocol as a seperate package #247

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ EXECUTABLE_TARGETS := $(patsubst cmd/%,cmd_%,$(wildcard cmd/*))

default: cmd

dev: cmd lint test

cache: build lint test
dev: build lint test

cmd: $(EXECUTABLE_TARGETS)

Expand All @@ -55,8 +53,7 @@ gocovmerge:
tidy:
go mod tidy
cd lib && go mod tidy

cache:
build:
go build ./...
cd lib && go build ./...

Expand Down
7 changes: 4 additions & 3 deletions pkg/proxy/backend/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/pingcap/TiProxy/lib/util/errors"
pnet "github.com/pingcap/TiProxy/pkg/proxy/net"
"github.com/pingcap/TiProxy/pkg/proxy/proxyprotocol"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/util/hack"
"go.uber.org/zap"
Expand Down Expand Up @@ -65,14 +66,14 @@ func (auth *Authenticator) writeProxyProtocol(clientIO, backendIO *pnet.PacketIO
if auth.proxyProtocol {
proxy := clientIO.Proxy()
if proxy == nil {
proxy = &pnet.Proxy{
proxy = &proxyprotocol.Proxy{
SrcAddress: clientIO.RemoteAddr(),
DstAddress: backendIO.RemoteAddr(),
Version: pnet.ProxyVersion2,
Version: proxyprotocol.ProxyVersion2,
}
}
// either from another proxy or directly from clients, we are acting as a proxy
proxy.Command = pnet.ProxyCommandProxy
proxy.Command = proxyprotocol.ProxyCommandProxy
if err := backendIO.WriteProxyV2(proxy); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/proxy/net/packetio.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ import (
"github.com/pingcap/TiProxy/lib/config"
"github.com/pingcap/TiProxy/lib/util/errors"
"github.com/pingcap/TiProxy/pkg/proxy/keepalive"
"github.com/pingcap/TiProxy/pkg/proxy/proxyprotocol"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/util/dbterror"
)

var (
errInvalidSequence = dbterror.ClassServer.NewStd(errno.ErrInvalidSequence)
proxyV2Magic = []byte{0xD, 0xA, 0xD, 0xA, 0x0, 0xD, 0xA, 0x51, 0x55, 0x49, 0x54, 0xA}
)

const (
Expand Down Expand Up @@ -84,7 +84,7 @@ type PacketIO struct {
rawConn net.Conn
buf *bufio.ReadWriter
proxyInited atomic.Bool
proxy *Proxy
proxy *proxyprotocol.Proxy
remoteAddr net.Addr
wrap error
sequence uint8
Expand Down Expand Up @@ -117,7 +117,7 @@ func (p *PacketIO) wrapErr(err error) error {
}

// Proxy returned parsed proxy header from clients if any.
func (p *PacketIO) Proxy() *Proxy {
func (p *PacketIO) Proxy() *proxyprotocol.Proxy {
if p.proxyInited.Load() {
return p.proxy
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func (p *PacketIO) readOnePacket() ([]byte, bool, error) {
// probe proxy V2
refill := false
if !p.proxyInited.Load() {
if bytes.Equal(header[:], proxyV2Magic[:4]) {
if bytes.Equal(header[:], proxyprotocol.MagicV2[:4]) {
proxyHeader, err := p.parseProxyV2()
if err != nil {
return nil, false, errors.Wrap(ErrReadConn, err)
Expand Down
15 changes: 7 additions & 8 deletions pkg/proxy/net/packetio_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package net

import (
"net"

"github.com/pingcap/TiProxy/pkg/proxy/proxyprotocol"
)

type PacketIOption = func(*PacketIO)
Expand All @@ -31,13 +33,17 @@ func WithWrapError(err error) func(pi *PacketIO) {
}

// WithRemoteAddr
var _ net.Addr = &originAddr{}
var _ proxyprotocol.AddressWrapper = &originAddr{}

type originAddr struct {
net.Addr
addr string
}

func (o *originAddr) Unwrap() net.Addr {
return o.Addr
}

func (o *originAddr) String() string {
return o.addr
}
Expand All @@ -47,10 +53,3 @@ func WithRemoteAddr(readdr string, addr net.Addr) func(pi *PacketIO) {
pi.remoteAddr = &originAddr{Addr: addr, addr: readdr}
}
}

func unwrapOriginAddr(addr net.Addr) net.Addr {
if oaddr, ok := addr.(*originAddr); ok {
return oaddr.Addr
}
return addr
}
Loading