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

support simple proxy method: RunSimpleCopy #854

Closed
wants to merge 2 commits into from
Closed
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
28 changes: 14 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ jobs:
test:
strategy:
matrix:
go: [ "1.21", "1.20", "1.19" ]
os: [ ubuntu-22.04, ubuntu-20.04 ]
go: [ "1.20"]
os: [ ubuntu-22.04 ]
name: Tests Go ${{ matrix.go }} on ${{ matrix.os }} # This name is used in main branch protection rules
runs-on: ${{ matrix.os }}

Expand Down Expand Up @@ -42,22 +42,22 @@ jobs:
go test $(go list ./... | grep -v canal)
go test $(go list ./... | grep canal)

golangci:
name: golangci
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: latest
args: --timeout=3m
# golangci:
# name: golangci
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - name: golangci-lint
# uses: golangci/golangci-lint-action@v2
# with:
# version: latest
# args: --timeout=3m

platforms:
strategy:
matrix:
arch: [ "amd64", "arm64" ]
os: [ "linux", "freebsd", "darwin" ]
arch: [ "amd64"]
os: [ "linux"]
name: platforms
runs-on: ubuntu-latest
steps:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var
bin
.idea
.idea
_build
5 changes: 5 additions & 0 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"sync/atomic"

"github.com/siddontang/go-log/log"
"github.com/siddontang/go/sync2"

. "github.com/go-mysql-org/go-mysql/mysql"
Expand All @@ -31,6 +32,9 @@ type Conn struct {
cachingSha2FullAuth bool

h Handler
// net connection
// connGetter NetConnGetter
db string

stmts map[uint32]*Stmt
stmtID uint32
Expand Down Expand Up @@ -92,6 +96,7 @@ func NewCustomizedConn(conn net.Conn, serverConf *Server, p CredentialProvider,
c.closed.Set(false)

if err := c.handshake(); err != nil {
log.Error(err)
c.Close()
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions server/handshake_resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func (c *Conn) readDb(data []byte, pos int) (int, error) {
db := string(data[pos : pos+bytes.IndexByte(data[pos:], 0x00)])
pos += len(db) + 1

// save db name
c.db = db

if err := c.h.UseDB(db); err != nil {
return 0, err
}
Expand Down
36 changes: 36 additions & 0 deletions server/simple_example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"net"

"github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/server"
"github.com/siddontang/go-log/log"
)

func main() {
l, _ := net.Listen("tcp", "127.0.0.1:4306")
provider := server.NewInMemoryProvider()
provider.AddUser("root", "root")
// var tlsConf = server.NewServerTLSConfig(test_keys.CaPem, test_keys.CertPem, test_keys.KeyPem, tls.VerifyClientCertIfGiven)
for {
c, _ := l.Accept()
go func() {
// Create a connection with user root and an empty password.
// You can use your own handler to handle command here.
svr := server.NewServer("8.0.12", mysql.DEFAULT_COLLATION_ID, mysql.AUTH_CACHING_SHA2_PASSWORD, nil, nil)
conn, err := server.NewCustomizedConn(c, svr, provider, server.EmptyHandler{})

if err != nil {
log.Errorf("Connection error: %v", err)
return
}

err = conn.RunSimpleCopy()
if err != nil {
log.Errorf(`simple proxy error: %v`, err)
return
}
}()
}
}
74 changes: 74 additions & 0 deletions server/simple_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package server

import (
"fmt"
"io"
"net"

"github.com/go-mysql-org/go-mysql/client"
"github.com/siddontang/go-log/log"
)

const (
BackendAddr string = "backend_addr"
BackendUser string = "backend_user"
BackendPasswd string = "backend_passwd"
)

type NetConnGetter interface {
GetConnection() net.Conn
}

// RunSimpleCopy blocks until the front/back connection is close.
func (c *Conn) RunSimpleCopy() (err error) {
if c.Conn == nil {
return fmt.Errorf("connection closed")
}

defer func() {
c.writeError(err)
c.Conn.Close()
}()

// get backend connection info
var (
addr, user, passwd string
backConn *client.Conn
)

addr = c.attributes[BackendAddr]
user = c.attributes[BackendUser]
passwd = c.attributes[BackendPasswd]

if addr == "" || user == "" {
err = fmt.Errorf("connection attributes is invalid: %+v", c.attributes)
return err
}

log.Infof("client %s try to connect backend(%s, %s, %s)", c.RemoteAddr().String(), addr, user, passwd)

backConn, err = client.Connect(addr, user, passwd, c.db)
if err != nil {
return err
}
defer backConn.Close()

// connect 2 connections

var errChan = make(chan error)
cpFunc := func(dst io.Writer, src io.Reader) {
_, err = io.Copy(dst, src)
errChan <- err
}
go cpFunc(c.Conn, backConn)
go cpFunc(backConn, c.Conn)
err = <-errChan
if err != nil {
err = fmt.Errorf("unexpected disconnection: %+v", err)
return err
}

log.Info("connection is closed on purpose")

return nil
}