-
Notifications
You must be signed in to change notification settings - Fork 27
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: simplified packetIO and proxyv2 en(de)coding #17
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f768834
util: refine error package
xhebox 2a1c0bb
proxy: refine packetio API
xhebox e958f0f
proxy: safer probe of proxy header
xhebox d4bfb15
proxy: only flush for handshake
xhebox e9c58d1
proxy: revert flush on close for now
xhebox 54d50cc
proxy: use consts from mysql
xhebox 74cac86
proxy: fix writing more than maxpayload
xhebox b6be517
proxy: disable proxy probe now
xhebox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// 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. | ||
|
||
package net | ||
|
||
import "github.com/pingcap/TiProxy/pkg/util/errors" | ||
|
||
var ( | ||
ErrExpectSSLRequest = errors.New("expect a SSLRequest packet") | ||
ErrReadConn = errors.New("failed to read the connection") | ||
ErrWriteConn = errors.New("failed to write the connection") | ||
ErrFlushConn = errors.New("failed to flush the connection") | ||
ErrCloseConn = errors.New("failed to close the connection") | ||
ErrHandshakeTLS = errors.New("failed to complete tls handshake") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// 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. | ||
|
||
package net | ||
|
||
import ( | ||
"encoding/binary" | ||
|
||
"github.com/pingcap/TiProxy/pkg/util/errors" | ||
"github.com/pingcap/tidb/parser/mysql" | ||
) | ||
|
||
func (p *PacketIO) WriteInitialHandshake(capability uint32, status uint16, salt []byte) error { | ||
data := make([]byte, 0, 128) | ||
|
||
// min version 10 | ||
data = append(data, 10) | ||
// server version[NUL] | ||
data = append(data, serverVersion...) | ||
data = append(data, 0) | ||
// connection id | ||
data = append(data, byte(capability), byte(capability>>8), byte(capability>>16), byte(capability>>24)) | ||
// auth-plugin-data-part-1 | ||
data = append(data, salt[:]...) | ||
// filler [00] | ||
data = append(data, 0) | ||
// capability flag lower 2 bytes, using default capability here | ||
data = append(data, byte(capability), byte(capability>>8)) | ||
// charset | ||
data = append(data, utf8mb4BinID) | ||
// status | ||
data = append(data, byte(status), byte(status>>8)) | ||
// capability flag upper 2 bytes, using default capability here | ||
data = append(data, byte(capability>>16), byte(capability>>24)) | ||
// length of auth-plugin-data | ||
data = append(data, byte(len(salt)+1)) | ||
// reserved 10 [00] | ||
data = append(data, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | ||
// auth-plugin-data-part-2 | ||
data = append(data, salt[8:]...) | ||
data = append(data, 0) | ||
// auth-plugin name | ||
data = append(data, []byte("mysql_native_password")...) | ||
data = append(data, 0) | ||
|
||
if err := p.WritePacket(data, true); err != nil { | ||
return err | ||
} | ||
|
||
return p.Flush() | ||
} | ||
|
||
func (p *PacketIO) ReadSSLRequest() ([]byte, error) { | ||
xhebox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pkt, err := p.ReadPacket() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(pkt) < 2 { | ||
return nil, errors.WithStack(errors.Errorf("%w: but got less than 2 bytes", ErrExpectSSLRequest)) | ||
} | ||
|
||
capability := uint32(binary.LittleEndian.Uint16(pkt[:2])) | ||
if capability&mysql.ClientSSL == 0 { | ||
return nil, errors.WithStack(errors.Errorf("%w: but capability flags has no SSL", ErrExpectSSLRequest)) | ||
} | ||
|
||
return pkt, nil | ||
} | ||
|
||
func (p *PacketIO) WriteErrPacket(code uint16, msg string) error { | ||
data := make([]byte, 0, 64) | ||
data = append(data, 0xff) | ||
data = append(data, byte(code), byte(code>>8)) | ||
// fill spaces for 421 protocol for compatibility | ||
data = append(data, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20) | ||
data = append(data, msg...) | ||
|
||
if err := p.WritePacket(data, true); err != nil { | ||
return err | ||
} | ||
|
||
return p.Flush() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can merge the write and following flush() together here. But it's OK, I can do it in the next PR.