mirror of
https://github.com/luscis/openlan.git
synced 2025-10-07 01:22:51 +08:00
clone from danieldin95
This commit is contained in:
202
vendor/github.com/shadowsocks/go-shadowsocks2/LICENSE
generated
vendored
Normal file
202
vendor/github.com/shadowsocks/go-shadowsocks2/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
109
vendor/github.com/shadowsocks/go-shadowsocks2/core/cipher.go
generated
vendored
Normal file
109
vendor/github.com/shadowsocks/go-shadowsocks2/core/cipher.go
generated
vendored
Normal file
@@ -0,0 +1,109 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"errors"
|
||||
"net"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/shadowsocks/go-shadowsocks2/shadowaead"
|
||||
)
|
||||
|
||||
type Cipher interface {
|
||||
StreamConnCipher
|
||||
PacketConnCipher
|
||||
}
|
||||
|
||||
type StreamConnCipher interface {
|
||||
StreamConn(net.Conn) net.Conn
|
||||
}
|
||||
|
||||
type PacketConnCipher interface {
|
||||
PacketConn(net.PacketConn) net.PacketConn
|
||||
}
|
||||
|
||||
// ErrCipherNotSupported occurs when a cipher is not supported (likely because of security concerns).
|
||||
var ErrCipherNotSupported = errors.New("cipher not supported")
|
||||
|
||||
const (
|
||||
aeadAes128Gcm = "AEAD_AES_128_GCM"
|
||||
aeadAes256Gcm = "AEAD_AES_256_GCM"
|
||||
aeadChacha20Poly1305 = "AEAD_CHACHA20_POLY1305"
|
||||
)
|
||||
|
||||
// List of AEAD ciphers: key size in bytes and constructor
|
||||
var aeadList = map[string]struct {
|
||||
KeySize int
|
||||
New func([]byte) (shadowaead.Cipher, error)
|
||||
}{
|
||||
aeadAes128Gcm: {16, shadowaead.AESGCM},
|
||||
aeadAes256Gcm: {32, shadowaead.AESGCM},
|
||||
aeadChacha20Poly1305: {32, shadowaead.Chacha20Poly1305},
|
||||
}
|
||||
|
||||
// ListCipher returns a list of available cipher names sorted alphabetically.
|
||||
func ListCipher() []string {
|
||||
var l []string
|
||||
for k := range aeadList {
|
||||
l = append(l, k)
|
||||
}
|
||||
sort.Strings(l)
|
||||
return l
|
||||
}
|
||||
|
||||
// PickCipher returns a Cipher of the given name. Derive key from password if given key is empty.
|
||||
func PickCipher(name string, key []byte, password string) (Cipher, error) {
|
||||
name = strings.ToUpper(name)
|
||||
|
||||
switch name {
|
||||
case "DUMMY":
|
||||
return &dummy{}, nil
|
||||
case "CHACHA20-IETF-POLY1305":
|
||||
name = aeadChacha20Poly1305
|
||||
case "AES-128-GCM":
|
||||
name = aeadAes128Gcm
|
||||
case "AES-256-GCM":
|
||||
name = aeadAes256Gcm
|
||||
}
|
||||
|
||||
if choice, ok := aeadList[name]; ok {
|
||||
if len(key) == 0 {
|
||||
key = kdf(password, choice.KeySize)
|
||||
}
|
||||
if len(key) != choice.KeySize {
|
||||
return nil, shadowaead.KeySizeError(choice.KeySize)
|
||||
}
|
||||
aead, err := choice.New(key)
|
||||
return &aeadCipher{aead}, err
|
||||
}
|
||||
|
||||
return nil, ErrCipherNotSupported
|
||||
}
|
||||
|
||||
type aeadCipher struct{ shadowaead.Cipher }
|
||||
|
||||
func (aead *aeadCipher) StreamConn(c net.Conn) net.Conn { return shadowaead.NewConn(c, aead) }
|
||||
func (aead *aeadCipher) PacketConn(c net.PacketConn) net.PacketConn {
|
||||
return shadowaead.NewPacketConn(c, aead)
|
||||
}
|
||||
|
||||
// dummy cipher does not encrypt
|
||||
type dummy struct{}
|
||||
|
||||
func (dummy) StreamConn(c net.Conn) net.Conn { return c }
|
||||
func (dummy) PacketConn(c net.PacketConn) net.PacketConn { return c }
|
||||
|
||||
// key-derivation function from original Shadowsocks
|
||||
func kdf(password string, keyLen int) []byte {
|
||||
var b, prev []byte
|
||||
h := md5.New()
|
||||
for len(b) < keyLen {
|
||||
h.Write(prev)
|
||||
h.Write([]byte(password))
|
||||
b = h.Sum(b)
|
||||
prev = b[len(b)-h.Size():]
|
||||
h.Reset()
|
||||
}
|
||||
return b[:keyLen]
|
||||
}
|
2
vendor/github.com/shadowsocks/go-shadowsocks2/core/doc.go
generated
vendored
Normal file
2
vendor/github.com/shadowsocks/go-shadowsocks2/core/doc.go
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
// Package core implements essential parts of Shadowsocks
|
||||
package core
|
8
vendor/github.com/shadowsocks/go-shadowsocks2/core/packet.go
generated
vendored
Normal file
8
vendor/github.com/shadowsocks/go-shadowsocks2/core/packet.go
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
package core
|
||||
|
||||
import "net"
|
||||
|
||||
func ListenPacket(network, address string, ciph PacketConnCipher) (net.PacketConn, error) {
|
||||
c, err := net.ListenPacket(network, address)
|
||||
return ciph.PacketConn(c), err
|
||||
}
|
23
vendor/github.com/shadowsocks/go-shadowsocks2/core/stream.go
generated
vendored
Normal file
23
vendor/github.com/shadowsocks/go-shadowsocks2/core/stream.go
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
package core
|
||||
|
||||
import "net"
|
||||
|
||||
type listener struct {
|
||||
net.Listener
|
||||
StreamConnCipher
|
||||
}
|
||||
|
||||
func Listen(network, address string, ciph StreamConnCipher) (net.Listener, error) {
|
||||
l, err := net.Listen(network, address)
|
||||
return &listener{l, ciph}, err
|
||||
}
|
||||
|
||||
func (l *listener) Accept() (net.Conn, error) {
|
||||
c, err := l.Listener.Accept()
|
||||
return l.StreamConn(c), err
|
||||
}
|
||||
|
||||
func Dial(network, address string, ciph StreamConnCipher) (net.Conn, error) {
|
||||
c, err := net.Dial(network, address)
|
||||
return ciph.StreamConn(c), err
|
||||
}
|
92
vendor/github.com/shadowsocks/go-shadowsocks2/internal/bloomring.go
generated
vendored
Normal file
92
vendor/github.com/shadowsocks/go-shadowsocks2/internal/bloomring.go
generated
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"hash/fnv"
|
||||
"sync"
|
||||
|
||||
"github.com/riobard/go-bloom"
|
||||
)
|
||||
|
||||
// simply use Double FNV here as our Bloom Filter hash
|
||||
func doubleFNV(b []byte) (uint64, uint64) {
|
||||
hx := fnv.New64()
|
||||
hx.Write(b)
|
||||
x := hx.Sum64()
|
||||
hy := fnv.New64a()
|
||||
hy.Write(b)
|
||||
y := hy.Sum64()
|
||||
return x, y
|
||||
}
|
||||
|
||||
type BloomRing struct {
|
||||
slotCapacity int
|
||||
slotPosition int
|
||||
slotCount int
|
||||
entryCounter int
|
||||
slots []bloom.Filter
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func NewBloomRing(slot, capacity int, falsePositiveRate float64) *BloomRing {
|
||||
// Calculate entries for each slot
|
||||
r := &BloomRing{
|
||||
slotCapacity: capacity / slot,
|
||||
slotCount: slot,
|
||||
slots: make([]bloom.Filter, slot),
|
||||
}
|
||||
for i := 0; i < slot; i++ {
|
||||
r.slots[i] = bloom.New(r.slotCapacity, falsePositiveRate, doubleFNV)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *BloomRing) Add(b []byte) {
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
r.add(b)
|
||||
}
|
||||
|
||||
func (r *BloomRing) add(b []byte) {
|
||||
slot := r.slots[r.slotPosition]
|
||||
if r.entryCounter > r.slotCapacity {
|
||||
// Move to next slot and reset
|
||||
r.slotPosition = (r.slotPosition + 1) % r.slotCount
|
||||
slot = r.slots[r.slotPosition]
|
||||
slot.Reset()
|
||||
r.entryCounter = 0
|
||||
}
|
||||
r.entryCounter++
|
||||
slot.Add(b)
|
||||
}
|
||||
|
||||
func (r *BloomRing) Test(b []byte) bool {
|
||||
if r == nil {
|
||||
return false
|
||||
}
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
test := r.test(b)
|
||||
return test
|
||||
}
|
||||
|
||||
func (r *BloomRing) test(b []byte) bool {
|
||||
for _, s := range r.slots {
|
||||
if s.Test(b) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *BloomRing) Check(b []byte) bool {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
if r.Test(b) {
|
||||
return true
|
||||
}
|
||||
r.Add(b)
|
||||
return false
|
||||
}
|
85
vendor/github.com/shadowsocks/go-shadowsocks2/internal/saltfilter.go
generated
vendored
Normal file
85
vendor/github.com/shadowsocks/go-shadowsocks2/internal/saltfilter.go
generated
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Those suggest value are all set according to
|
||||
// https://github.com/shadowsocks/shadowsocks-org/issues/44#issuecomment-281021054
|
||||
// Due to this package contains various internal implementation so const named with DefaultBR prefix
|
||||
const (
|
||||
DefaultSFCapacity = 1e6
|
||||
// FalsePositiveRate
|
||||
DefaultSFFPR = 1e-6
|
||||
DefaultSFSlot = 10
|
||||
)
|
||||
|
||||
const EnvironmentPrefix = "SHADOWSOCKS_"
|
||||
|
||||
// A shared instance used for checking salt repeat
|
||||
var saltfilter *BloomRing
|
||||
|
||||
// Used to initialize the saltfilter singleton only once.
|
||||
var initSaltfilterOnce sync.Once
|
||||
|
||||
// GetSaltFilterSingleton returns the BloomRing singleton,
|
||||
// initializing it on first call.
|
||||
func getSaltFilterSingleton() *BloomRing {
|
||||
initSaltfilterOnce.Do(func() {
|
||||
var (
|
||||
finalCapacity = DefaultSFCapacity
|
||||
finalFPR = DefaultSFFPR
|
||||
finalSlot = float64(DefaultSFSlot)
|
||||
)
|
||||
for _, opt := range []struct {
|
||||
ENVName string
|
||||
Target *float64
|
||||
}{
|
||||
{
|
||||
ENVName: "CAPACITY",
|
||||
Target: &finalCapacity,
|
||||
},
|
||||
{
|
||||
ENVName: "FPR",
|
||||
Target: &finalFPR,
|
||||
},
|
||||
{
|
||||
ENVName: "SLOT",
|
||||
Target: &finalSlot,
|
||||
},
|
||||
} {
|
||||
envKey := EnvironmentPrefix + "SF_" + opt.ENVName
|
||||
env := os.Getenv(envKey)
|
||||
if env != "" {
|
||||
p, err := strconv.ParseFloat(env, 64)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Invalid envrionment `%s` setting in saltfilter: %s", envKey, env))
|
||||
}
|
||||
*opt.Target = p
|
||||
}
|
||||
}
|
||||
// Support disable saltfilter by given a negative capacity
|
||||
if finalCapacity <= 0 {
|
||||
return
|
||||
}
|
||||
saltfilter = NewBloomRing(int(finalSlot), int(finalCapacity), finalFPR)
|
||||
})
|
||||
return saltfilter
|
||||
}
|
||||
|
||||
// TestSalt returns true if salt is repeated
|
||||
func TestSalt(b []byte) bool {
|
||||
return getSaltFilterSingleton().Test(b)
|
||||
}
|
||||
|
||||
// AddSalt salt to filter
|
||||
func AddSalt(b []byte) {
|
||||
getSaltFilterSingleton().Add(b)
|
||||
}
|
||||
|
||||
func CheckSalt(b []byte) bool {
|
||||
return getSaltFilterSingleton().Test(b)
|
||||
}
|
55
vendor/github.com/shadowsocks/go-shadowsocks2/nfutil/nf_linux.go
generated
vendored
Normal file
55
vendor/github.com/shadowsocks/go-shadowsocks2/nfutil/nf_linux.go
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
package nfutil
|
||||
|
||||
import (
|
||||
"net"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Get the original destination of a TCP connection redirected by Netfilter.
|
||||
func GetOrigDst(c *net.TCPConn, ipv6 bool) (*net.TCPAddr, error) {
|
||||
rc, err := c.SyscallConn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var addr *net.TCPAddr
|
||||
rc.Control(func(fd uintptr) {
|
||||
if ipv6 {
|
||||
addr, err = ipv6_getorigdst(fd)
|
||||
} else {
|
||||
addr, err = getorigdst(fd)
|
||||
}
|
||||
})
|
||||
return addr, err
|
||||
}
|
||||
|
||||
// Call getorigdst() from linux/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
|
||||
func getorigdst(fd uintptr) (*net.TCPAddr, error) {
|
||||
const _SO_ORIGINAL_DST = 80 // from linux/include/uapi/linux/netfilter_ipv4.h
|
||||
var raw syscall.RawSockaddrInet4
|
||||
siz := unsafe.Sizeof(raw)
|
||||
if err := socketcall(GETSOCKOPT, fd, syscall.IPPROTO_IP, _SO_ORIGINAL_DST, uintptr(unsafe.Pointer(&raw)), uintptr(unsafe.Pointer(&siz)), 0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var addr net.TCPAddr
|
||||
addr.IP = raw.Addr[:]
|
||||
port := (*[2]byte)(unsafe.Pointer(&raw.Port)) // raw.Port is big-endian
|
||||
addr.Port = int(port[0])<<8 | int(port[1])
|
||||
return &addr, nil
|
||||
}
|
||||
|
||||
// Call ipv6_getorigdst() from linux/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
|
||||
// NOTE: I haven't tried yet but it should work since Linux 3.8.
|
||||
func ipv6_getorigdst(fd uintptr) (*net.TCPAddr, error) {
|
||||
const _IP6T_SO_ORIGINAL_DST = 80 // from linux/include/uapi/linux/netfilter_ipv6/ip6_tables.h
|
||||
var raw syscall.RawSockaddrInet6
|
||||
siz := unsafe.Sizeof(raw)
|
||||
if err := socketcall(GETSOCKOPT, fd, syscall.IPPROTO_IPV6, _IP6T_SO_ORIGINAL_DST, uintptr(unsafe.Pointer(&raw)), uintptr(unsafe.Pointer(&siz)), 0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var addr net.TCPAddr
|
||||
addr.IP = raw.Addr[:]
|
||||
port := (*[2]byte)(unsafe.Pointer(&raw.Port)) // raw.Port is big-endian
|
||||
addr.Port = int(port[0])<<8 | int(port[1])
|
||||
return &addr, nil
|
||||
}
|
17
vendor/github.com/shadowsocks/go-shadowsocks2/nfutil/socketcall_linux_386.go
generated
vendored
Normal file
17
vendor/github.com/shadowsocks/go-shadowsocks2/nfutil/socketcall_linux_386.go
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
package nfutil
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const GETSOCKOPT = 15 // https://golang.org/src/syscall/syscall_linux_386.go#L183
|
||||
|
||||
func socketcall(call, a0, a1, a2, a3, a4, a5 uintptr) error {
|
||||
var a [6]uintptr
|
||||
a[0], a[1], a[2], a[3], a[4], a[5] = a0, a1, a2, a3, a4, a5
|
||||
if _, _, errno := syscall.Syscall6(syscall.SYS_SOCKETCALL, call, uintptr(unsafe.Pointer(&a)), 0, 0, 0, 0); errno != 0 {
|
||||
return errno
|
||||
}
|
||||
return nil
|
||||
}
|
14
vendor/github.com/shadowsocks/go-shadowsocks2/nfutil/socketcall_linux_other.go
generated
vendored
Normal file
14
vendor/github.com/shadowsocks/go-shadowsocks2/nfutil/socketcall_linux_other.go
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
// +build linux,!386
|
||||
|
||||
package nfutil
|
||||
|
||||
import "syscall"
|
||||
|
||||
const GETSOCKOPT = syscall.SYS_GETSOCKOPT
|
||||
|
||||
func socketcall(call, a0, a1, a2, a3, a4, a5 uintptr) error {
|
||||
if _, _, errno := syscall.Syscall6(call, a0, a1, a2, a3, a4, a5); errno != 0 {
|
||||
return errno
|
||||
}
|
||||
return nil
|
||||
}
|
51
vendor/github.com/shadowsocks/go-shadowsocks2/pfutil/pf_darwin.go
generated
vendored
Normal file
51
vendor/github.com/shadowsocks/go-shadowsocks2/pfutil/pf_darwin.go
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
package pfutil
|
||||
|
||||
import (
|
||||
"net"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func NatLookup(c *net.TCPConn) (*net.TCPAddr, error) {
|
||||
const (
|
||||
PF_INOUT = 0
|
||||
PF_IN = 1
|
||||
PF_OUT = 2
|
||||
IOC_OUT = 0x40000000
|
||||
IOC_IN = 0x80000000
|
||||
IOC_INOUT = IOC_IN | IOC_OUT
|
||||
IOCPARM_MASK = 0x1FFF
|
||||
LEN = 4*16 + 4*4 + 4*1
|
||||
// #define _IOC(inout,group,num,len) (inout | ((len & IOCPARM_MASK) << 16) | ((group) << 8) | (num))
|
||||
// #define _IOWR(g,n,t) _IOC(IOC_INOUT, (g), (n), sizeof(t))
|
||||
// #define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook)
|
||||
DIOCNATLOOK = IOC_INOUT | ((LEN & IOCPARM_MASK) << 16) | ('D' << 8) | 23
|
||||
)
|
||||
fd, err := syscall.Open("/dev/pf", 0, syscall.O_RDONLY)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer syscall.Close(fd)
|
||||
nl := struct { // struct pfioc_natlook
|
||||
saddr, daddr, rsaddr, rdaddr [16]byte
|
||||
sxport, dxport, rsxport, rdxport [4]byte
|
||||
af, proto, protoVariant, direction uint8
|
||||
}{
|
||||
af: syscall.AF_INET,
|
||||
proto: syscall.IPPROTO_TCP,
|
||||
direction: PF_OUT,
|
||||
}
|
||||
saddr := c.RemoteAddr().(*net.TCPAddr)
|
||||
daddr := c.LocalAddr().(*net.TCPAddr)
|
||||
copy(nl.saddr[:], saddr.IP)
|
||||
copy(nl.daddr[:], daddr.IP)
|
||||
nl.sxport[0], nl.sxport[1] = byte(saddr.Port>>8), byte(saddr.Port)
|
||||
nl.dxport[0], nl.dxport[1] = byte(daddr.Port>>8), byte(daddr.Port)
|
||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), DIOCNATLOOK, uintptr(unsafe.Pointer(&nl))); errno != 0 {
|
||||
return nil, errno
|
||||
}
|
||||
var addr net.TCPAddr
|
||||
addr.IP = nl.rdaddr[:4]
|
||||
addr.Port = int(nl.rdxport[0])<<8 | int(nl.rdxport[1])
|
||||
return &addr, nil
|
||||
}
|
87
vendor/github.com/shadowsocks/go-shadowsocks2/shadowaead/cipher.go
generated
vendored
Normal file
87
vendor/github.com/shadowsocks/go-shadowsocks2/shadowaead/cipher.go
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
package shadowaead
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/sha1"
|
||||
"errors"
|
||||
"io"
|
||||
"strconv"
|
||||
|
||||
"golang.org/x/crypto/chacha20poly1305"
|
||||
"golang.org/x/crypto/hkdf"
|
||||
)
|
||||
|
||||
// ErrRepeatedSalt means detected a reused salt
|
||||
var ErrRepeatedSalt = errors.New("repeated salt detected")
|
||||
|
||||
type Cipher interface {
|
||||
KeySize() int
|
||||
SaltSize() int
|
||||
Encrypter(salt []byte) (cipher.AEAD, error)
|
||||
Decrypter(salt []byte) (cipher.AEAD, error)
|
||||
}
|
||||
|
||||
type KeySizeError int
|
||||
|
||||
func (e KeySizeError) Error() string {
|
||||
return "key size error: need " + strconv.Itoa(int(e)) + " bytes"
|
||||
}
|
||||
|
||||
func hkdfSHA1(secret, salt, info, outkey []byte) {
|
||||
r := hkdf.New(sha1.New, secret, salt, info)
|
||||
if _, err := io.ReadFull(r, outkey); err != nil {
|
||||
panic(err) // should never happen
|
||||
}
|
||||
}
|
||||
|
||||
type metaCipher struct {
|
||||
psk []byte
|
||||
makeAEAD func(key []byte) (cipher.AEAD, error)
|
||||
}
|
||||
|
||||
func (a *metaCipher) KeySize() int { return len(a.psk) }
|
||||
func (a *metaCipher) SaltSize() int {
|
||||
if ks := a.KeySize(); ks > 16 {
|
||||
return ks
|
||||
}
|
||||
return 16
|
||||
}
|
||||
func (a *metaCipher) Encrypter(salt []byte) (cipher.AEAD, error) {
|
||||
subkey := make([]byte, a.KeySize())
|
||||
hkdfSHA1(a.psk, salt, []byte("ss-subkey"), subkey)
|
||||
return a.makeAEAD(subkey)
|
||||
}
|
||||
func (a *metaCipher) Decrypter(salt []byte) (cipher.AEAD, error) {
|
||||
subkey := make([]byte, a.KeySize())
|
||||
hkdfSHA1(a.psk, salt, []byte("ss-subkey"), subkey)
|
||||
return a.makeAEAD(subkey)
|
||||
}
|
||||
|
||||
func aesGCM(key []byte) (cipher.AEAD, error) {
|
||||
blk, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cipher.NewGCM(blk)
|
||||
}
|
||||
|
||||
// AESGCM creates a new Cipher with a pre-shared key. len(psk) must be
|
||||
// one of 16, 24, or 32 to select AES-128/196/256-GCM.
|
||||
func AESGCM(psk []byte) (Cipher, error) {
|
||||
switch l := len(psk); l {
|
||||
case 16, 24, 32: // AES 128/196/256
|
||||
default:
|
||||
return nil, aes.KeySizeError(l)
|
||||
}
|
||||
return &metaCipher{psk: psk, makeAEAD: aesGCM}, nil
|
||||
}
|
||||
|
||||
// Chacha20Poly1305 creates a new Cipher with a pre-shared key. len(psk)
|
||||
// must be 32.
|
||||
func Chacha20Poly1305(psk []byte) (Cipher, error) {
|
||||
if len(psk) != chacha20poly1305.KeySize {
|
||||
return nil, KeySizeError(chacha20poly1305.KeySize)
|
||||
}
|
||||
return &metaCipher{psk: psk, makeAEAD: chacha20poly1305.New}, nil
|
||||
}
|
35
vendor/github.com/shadowsocks/go-shadowsocks2/shadowaead/doc.go
generated
vendored
Normal file
35
vendor/github.com/shadowsocks/go-shadowsocks2/shadowaead/doc.go
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
Package shadowaead implements a simple AEAD-protected secure protocol.
|
||||
|
||||
In general, there are two types of connections: stream-oriented and packet-oriented.
|
||||
Stream-oriented connections (e.g. TCP) assume reliable and orderly delivery of bytes.
|
||||
Packet-oriented connections (e.g. UDP) assume unreliable and out-of-order delivery of packets,
|
||||
where each packet is either delivered intact or lost.
|
||||
|
||||
An encrypted stream starts with a random salt to derive a session key, followed by any number of
|
||||
encrypted records. Each encrypted record has the following structure:
|
||||
|
||||
[encrypted payload length]
|
||||
[payload length tag]
|
||||
[encrypted payload]
|
||||
[payload tag]
|
||||
|
||||
Payload length is 2-byte unsigned big-endian integer capped at 0x3FFF (16383).
|
||||
The higher 2 bits are reserved and must be set to zero. The first AEAD encrypt/decrypt
|
||||
operation uses a counting nonce starting from 0. After each encrypt/decrypt operation,
|
||||
the nonce is incremented by one as if it were an unsigned little-endian integer.
|
||||
|
||||
|
||||
Each encrypted packet transmitted on a packet-oriented connection has the following structure:
|
||||
|
||||
[random salt]
|
||||
[encrypted payload]
|
||||
[payload tag]
|
||||
|
||||
The salt is used to derive a subkey to initiate an AEAD. Packets are encrypted/decrypted independently
|
||||
using zero nonce.
|
||||
|
||||
In both stream-oriented and packet-oriented connections, length of nonce and tag varies
|
||||
depending on which AEAD is used. Salt should be at least 16-byte long.
|
||||
*/
|
||||
package shadowaead
|
103
vendor/github.com/shadowsocks/go-shadowsocks2/shadowaead/packet.go
generated
vendored
Normal file
103
vendor/github.com/shadowsocks/go-shadowsocks2/shadowaead/packet.go
generated
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
package shadowaead
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"github.com/shadowsocks/go-shadowsocks2/internal"
|
||||
)
|
||||
|
||||
// ErrShortPacket means that the packet is too short for a valid encrypted packet.
|
||||
var ErrShortPacket = errors.New("short packet")
|
||||
|
||||
var _zerononce [128]byte // read-only. 128 bytes is more than enough.
|
||||
|
||||
// Pack encrypts plaintext using Cipher with a randomly generated salt and
|
||||
// returns a slice of dst containing the encrypted packet and any error occurred.
|
||||
// Ensure len(dst) >= ciph.SaltSize() + len(plaintext) + aead.Overhead().
|
||||
func Pack(dst, plaintext []byte, ciph Cipher) ([]byte, error) {
|
||||
saltSize := ciph.SaltSize()
|
||||
salt := dst[:saltSize]
|
||||
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
aead, err := ciph.Encrypter(salt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
internal.AddSalt(salt)
|
||||
|
||||
if len(dst) < saltSize+len(plaintext)+aead.Overhead() {
|
||||
return nil, io.ErrShortBuffer
|
||||
}
|
||||
b := aead.Seal(dst[saltSize:saltSize], _zerononce[:aead.NonceSize()], plaintext, nil)
|
||||
return dst[:saltSize+len(b)], nil
|
||||
}
|
||||
|
||||
// Unpack decrypts pkt using Cipher and returns a slice of dst containing the decrypted payload and any error occurred.
|
||||
// Ensure len(dst) >= len(pkt) - aead.SaltSize() - aead.Overhead().
|
||||
func Unpack(dst, pkt []byte, ciph Cipher) ([]byte, error) {
|
||||
saltSize := ciph.SaltSize()
|
||||
if len(pkt) < saltSize {
|
||||
return nil, ErrShortPacket
|
||||
}
|
||||
salt := pkt[:saltSize]
|
||||
aead, err := ciph.Decrypter(salt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if internal.CheckSalt(salt) {
|
||||
return nil, ErrRepeatedSalt
|
||||
}
|
||||
if len(pkt) < saltSize+aead.Overhead() {
|
||||
return nil, ErrShortPacket
|
||||
}
|
||||
if saltSize+len(dst)+aead.Overhead() < len(pkt) {
|
||||
return nil, io.ErrShortBuffer
|
||||
}
|
||||
b, err := aead.Open(dst[:0], _zerononce[:aead.NonceSize()], pkt[saltSize:], nil)
|
||||
return b, err
|
||||
}
|
||||
|
||||
type packetConn struct {
|
||||
net.PacketConn
|
||||
Cipher
|
||||
sync.Mutex
|
||||
buf []byte // write lock
|
||||
}
|
||||
|
||||
// NewPacketConn wraps a net.PacketConn with cipher
|
||||
func NewPacketConn(c net.PacketConn, ciph Cipher) net.PacketConn {
|
||||
const maxPacketSize = 64 * 1024
|
||||
return &packetConn{PacketConn: c, Cipher: ciph, buf: make([]byte, maxPacketSize)}
|
||||
}
|
||||
|
||||
// WriteTo encrypts b and write to addr using the embedded PacketConn.
|
||||
func (c *packetConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
buf, err := Pack(c.buf, b, c)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
_, err = c.PacketConn.WriteTo(buf, addr)
|
||||
return len(b), err
|
||||
}
|
||||
|
||||
// ReadFrom reads from the embedded PacketConn and decrypts into b.
|
||||
func (c *packetConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
n, addr, err := c.PacketConn.ReadFrom(b)
|
||||
if err != nil {
|
||||
return n, addr, err
|
||||
}
|
||||
bb, err := Unpack(b[c.Cipher.SaltSize():], b[:n], c)
|
||||
if err != nil {
|
||||
return n, addr, err
|
||||
}
|
||||
copy(b, bb)
|
||||
return len(bb), addr, err
|
||||
}
|
276
vendor/github.com/shadowsocks/go-shadowsocks2/shadowaead/stream.go
generated
vendored
Normal file
276
vendor/github.com/shadowsocks/go-shadowsocks2/shadowaead/stream.go
generated
vendored
Normal file
@@ -0,0 +1,276 @@
|
||||
package shadowaead
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/shadowsocks/go-shadowsocks2/internal"
|
||||
)
|
||||
|
||||
// payloadSizeMask is the maximum size of payload in bytes.
|
||||
const payloadSizeMask = 0x3FFF // 16*1024 - 1
|
||||
|
||||
type writer struct {
|
||||
io.Writer
|
||||
cipher.AEAD
|
||||
nonce []byte
|
||||
buf []byte
|
||||
}
|
||||
|
||||
// NewWriter wraps an io.Writer with AEAD encryption.
|
||||
func NewWriter(w io.Writer, aead cipher.AEAD) io.Writer { return newWriter(w, aead) }
|
||||
|
||||
func newWriter(w io.Writer, aead cipher.AEAD) *writer {
|
||||
return &writer{
|
||||
Writer: w,
|
||||
AEAD: aead,
|
||||
buf: make([]byte, 2+aead.Overhead()+payloadSizeMask+aead.Overhead()),
|
||||
nonce: make([]byte, aead.NonceSize()),
|
||||
}
|
||||
}
|
||||
|
||||
// Write encrypts b and writes to the embedded io.Writer.
|
||||
func (w *writer) Write(b []byte) (int, error) {
|
||||
n, err := w.ReadFrom(bytes.NewBuffer(b))
|
||||
return int(n), err
|
||||
}
|
||||
|
||||
// ReadFrom reads from the given io.Reader until EOF or error, encrypts and
|
||||
// writes to the embedded io.Writer. Returns number of bytes read from r and
|
||||
// any error encountered.
|
||||
func (w *writer) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
for {
|
||||
buf := w.buf
|
||||
payloadBuf := buf[2+w.Overhead() : 2+w.Overhead()+payloadSizeMask]
|
||||
nr, er := r.Read(payloadBuf)
|
||||
|
||||
if nr > 0 {
|
||||
n += int64(nr)
|
||||
buf = buf[:2+w.Overhead()+nr+w.Overhead()]
|
||||
payloadBuf = payloadBuf[:nr]
|
||||
buf[0], buf[1] = byte(nr>>8), byte(nr) // big-endian payload size
|
||||
w.Seal(buf[:0], w.nonce, buf[:2], nil)
|
||||
increment(w.nonce)
|
||||
|
||||
w.Seal(payloadBuf[:0], w.nonce, payloadBuf, nil)
|
||||
increment(w.nonce)
|
||||
|
||||
_, ew := w.Writer.Write(buf)
|
||||
if ew != nil {
|
||||
err = ew
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if er != nil {
|
||||
if er != io.EOF { // ignore EOF as per io.ReaderFrom contract
|
||||
err = er
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return n, err
|
||||
}
|
||||
|
||||
type reader struct {
|
||||
io.Reader
|
||||
cipher.AEAD
|
||||
nonce []byte
|
||||
buf []byte
|
||||
leftover []byte
|
||||
}
|
||||
|
||||
// NewReader wraps an io.Reader with AEAD decryption.
|
||||
func NewReader(r io.Reader, aead cipher.AEAD) io.Reader { return newReader(r, aead) }
|
||||
|
||||
func newReader(r io.Reader, aead cipher.AEAD) *reader {
|
||||
return &reader{
|
||||
Reader: r,
|
||||
AEAD: aead,
|
||||
buf: make([]byte, payloadSizeMask+aead.Overhead()),
|
||||
nonce: make([]byte, aead.NonceSize()),
|
||||
}
|
||||
}
|
||||
|
||||
// read and decrypt a record into the internal buffer. Return decrypted payload length and any error encountered.
|
||||
func (r *reader) read() (int, error) {
|
||||
// decrypt payload size
|
||||
buf := r.buf[:2+r.Overhead()]
|
||||
_, err := io.ReadFull(r.Reader, buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
_, err = r.Open(buf[:0], r.nonce, buf, nil)
|
||||
increment(r.nonce)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
size := (int(buf[0])<<8 + int(buf[1])) & payloadSizeMask
|
||||
|
||||
// decrypt payload
|
||||
buf = r.buf[:size+r.Overhead()]
|
||||
_, err = io.ReadFull(r.Reader, buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
_, err = r.Open(buf[:0], r.nonce, buf, nil)
|
||||
increment(r.nonce)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return size, nil
|
||||
}
|
||||
|
||||
// Read reads from the embedded io.Reader, decrypts and writes to b.
|
||||
func (r *reader) Read(b []byte) (int, error) {
|
||||
// copy decrypted bytes (if any) from previous record first
|
||||
if len(r.leftover) > 0 {
|
||||
n := copy(b, r.leftover)
|
||||
r.leftover = r.leftover[n:]
|
||||
return n, nil
|
||||
}
|
||||
|
||||
n, err := r.read()
|
||||
m := copy(b, r.buf[:n])
|
||||
if m < n { // insufficient len(b), keep leftover for next read
|
||||
r.leftover = r.buf[m:n]
|
||||
}
|
||||
return m, err
|
||||
}
|
||||
|
||||
// WriteTo reads from the embedded io.Reader, decrypts and writes to w until
|
||||
// there's no more data to write or when an error occurs. Return number of
|
||||
// bytes written to w and any error encountered.
|
||||
func (r *reader) WriteTo(w io.Writer) (n int64, err error) {
|
||||
// write decrypted bytes left over from previous record
|
||||
for len(r.leftover) > 0 {
|
||||
nw, ew := w.Write(r.leftover)
|
||||
r.leftover = r.leftover[nw:]
|
||||
n += int64(nw)
|
||||
if ew != nil {
|
||||
return n, ew
|
||||
}
|
||||
}
|
||||
|
||||
for {
|
||||
nr, er := r.read()
|
||||
if nr > 0 {
|
||||
nw, ew := w.Write(r.buf[:nr])
|
||||
n += int64(nw)
|
||||
|
||||
if ew != nil {
|
||||
err = ew
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if er != nil {
|
||||
if er != io.EOF { // ignore EOF as per io.Copy contract (using src.WriteTo shortcut)
|
||||
err = er
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return n, err
|
||||
}
|
||||
|
||||
// increment little-endian encoded unsigned integer b. Wrap around on overflow.
|
||||
func increment(b []byte) {
|
||||
for i := range b {
|
||||
b[i]++
|
||||
if b[i] != 0 {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type streamConn struct {
|
||||
net.Conn
|
||||
Cipher
|
||||
r *reader
|
||||
w *writer
|
||||
}
|
||||
|
||||
func (c *streamConn) initReader() error {
|
||||
salt := make([]byte, c.SaltSize())
|
||||
if _, err := io.ReadFull(c.Conn, salt); err != nil {
|
||||
return err
|
||||
}
|
||||
aead, err := c.Decrypter(salt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if internal.CheckSalt(salt) {
|
||||
return ErrRepeatedSalt
|
||||
}
|
||||
|
||||
c.r = newReader(c.Conn, aead)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *streamConn) Read(b []byte) (int, error) {
|
||||
if c.r == nil {
|
||||
if err := c.initReader(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return c.r.Read(b)
|
||||
}
|
||||
|
||||
func (c *streamConn) WriteTo(w io.Writer) (int64, error) {
|
||||
if c.r == nil {
|
||||
if err := c.initReader(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return c.r.WriteTo(w)
|
||||
}
|
||||
|
||||
func (c *streamConn) initWriter() error {
|
||||
salt := make([]byte, c.SaltSize())
|
||||
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
|
||||
return err
|
||||
}
|
||||
aead, err := c.Encrypter(salt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.Conn.Write(salt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
internal.AddSalt(salt)
|
||||
c.w = newWriter(c.Conn, aead)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *streamConn) Write(b []byte) (int, error) {
|
||||
if c.w == nil {
|
||||
if err := c.initWriter(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return c.w.Write(b)
|
||||
}
|
||||
|
||||
func (c *streamConn) ReadFrom(r io.Reader) (int64, error) {
|
||||
if c.w == nil {
|
||||
if err := c.initWriter(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return c.w.ReadFrom(r)
|
||||
}
|
||||
|
||||
// NewConn wraps a stream-oriented net.Conn with cipher.
|
||||
func NewConn(c net.Conn, ciph Cipher) net.Conn { return &streamConn{Conn: c, Cipher: ciph} }
|
214
vendor/github.com/shadowsocks/go-shadowsocks2/socks/socks.go
generated
vendored
Normal file
214
vendor/github.com/shadowsocks/go-shadowsocks2/socks/socks.go
generated
vendored
Normal file
@@ -0,0 +1,214 @@
|
||||
// Package socks implements essential parts of SOCKS protocol.
|
||||
package socks
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// UDPEnabled is the toggle for UDP support
|
||||
var UDPEnabled = false
|
||||
|
||||
// SOCKS request commands as defined in RFC 1928 section 4.
|
||||
const (
|
||||
CmdConnect = 1
|
||||
CmdBind = 2
|
||||
CmdUDPAssociate = 3
|
||||
)
|
||||
|
||||
// SOCKS address types as defined in RFC 1928 section 5.
|
||||
const (
|
||||
AtypIPv4 = 1
|
||||
AtypDomainName = 3
|
||||
AtypIPv6 = 4
|
||||
)
|
||||
|
||||
// Error represents a SOCKS error
|
||||
type Error byte
|
||||
|
||||
func (err Error) Error() string {
|
||||
return "SOCKS error: " + strconv.Itoa(int(err))
|
||||
}
|
||||
|
||||
// SOCKS errors as defined in RFC 1928 section 6.
|
||||
const (
|
||||
ErrGeneralFailure = Error(1)
|
||||
ErrConnectionNotAllowed = Error(2)
|
||||
ErrNetworkUnreachable = Error(3)
|
||||
ErrHostUnreachable = Error(4)
|
||||
ErrConnectionRefused = Error(5)
|
||||
ErrTTLExpired = Error(6)
|
||||
ErrCommandNotSupported = Error(7)
|
||||
ErrAddressNotSupported = Error(8)
|
||||
InfoUDPAssociate = Error(9)
|
||||
)
|
||||
|
||||
// MaxAddrLen is the maximum size of SOCKS address in bytes.
|
||||
const MaxAddrLen = 1 + 1 + 255 + 2
|
||||
|
||||
// Addr represents a SOCKS address as defined in RFC 1928 section 5.
|
||||
type Addr []byte
|
||||
|
||||
// String serializes SOCKS address a to string form.
|
||||
func (a Addr) String() string {
|
||||
var host, port string
|
||||
|
||||
switch a[0] { // address type
|
||||
case AtypDomainName:
|
||||
host = string(a[2 : 2+int(a[1])])
|
||||
port = strconv.Itoa((int(a[2+int(a[1])]) << 8) | int(a[2+int(a[1])+1]))
|
||||
case AtypIPv4:
|
||||
host = net.IP(a[1 : 1+net.IPv4len]).String()
|
||||
port = strconv.Itoa((int(a[1+net.IPv4len]) << 8) | int(a[1+net.IPv4len+1]))
|
||||
case AtypIPv6:
|
||||
host = net.IP(a[1 : 1+net.IPv6len]).String()
|
||||
port = strconv.Itoa((int(a[1+net.IPv6len]) << 8) | int(a[1+net.IPv6len+1]))
|
||||
}
|
||||
|
||||
return net.JoinHostPort(host, port)
|
||||
}
|
||||
|
||||
func readAddr(r io.Reader, b []byte) (Addr, error) {
|
||||
if len(b) < MaxAddrLen {
|
||||
return nil, io.ErrShortBuffer
|
||||
}
|
||||
_, err := io.ReadFull(r, b[:1]) // read 1st byte for address type
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch b[0] {
|
||||
case AtypDomainName:
|
||||
_, err = io.ReadFull(r, b[1:2]) // read 2nd byte for domain length
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = io.ReadFull(r, b[2:2+int(b[1])+2])
|
||||
return b[:1+1+int(b[1])+2], err
|
||||
case AtypIPv4:
|
||||
_, err = io.ReadFull(r, b[1:1+net.IPv4len+2])
|
||||
return b[:1+net.IPv4len+2], err
|
||||
case AtypIPv6:
|
||||
_, err = io.ReadFull(r, b[1:1+net.IPv6len+2])
|
||||
return b[:1+net.IPv6len+2], err
|
||||
}
|
||||
|
||||
return nil, ErrAddressNotSupported
|
||||
}
|
||||
|
||||
// ReadAddr reads just enough bytes from r to get a valid Addr.
|
||||
func ReadAddr(r io.Reader) (Addr, error) {
|
||||
return readAddr(r, make([]byte, MaxAddrLen))
|
||||
}
|
||||
|
||||
// SplitAddr slices a SOCKS address from beginning of b. Returns nil if failed.
|
||||
func SplitAddr(b []byte) Addr {
|
||||
addrLen := 1
|
||||
if len(b) < addrLen {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch b[0] {
|
||||
case AtypDomainName:
|
||||
if len(b) < 2 {
|
||||
return nil
|
||||
}
|
||||
addrLen = 1 + 1 + int(b[1]) + 2
|
||||
case AtypIPv4:
|
||||
addrLen = 1 + net.IPv4len + 2
|
||||
case AtypIPv6:
|
||||
addrLen = 1 + net.IPv6len + 2
|
||||
default:
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
if len(b) < addrLen {
|
||||
return nil
|
||||
}
|
||||
|
||||
return b[:addrLen]
|
||||
}
|
||||
|
||||
// ParseAddr parses the address in string s. Returns nil if failed.
|
||||
func ParseAddr(s string) Addr {
|
||||
var addr Addr
|
||||
host, port, err := net.SplitHostPort(s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
if ip4 := ip.To4(); ip4 != nil {
|
||||
addr = make([]byte, 1+net.IPv4len+2)
|
||||
addr[0] = AtypIPv4
|
||||
copy(addr[1:], ip4)
|
||||
} else {
|
||||
addr = make([]byte, 1+net.IPv6len+2)
|
||||
addr[0] = AtypIPv6
|
||||
copy(addr[1:], ip)
|
||||
}
|
||||
} else {
|
||||
if len(host) > 255 {
|
||||
return nil
|
||||
}
|
||||
addr = make([]byte, 1+1+len(host)+2)
|
||||
addr[0] = AtypDomainName
|
||||
addr[1] = byte(len(host))
|
||||
copy(addr[2:], host)
|
||||
}
|
||||
|
||||
portnum, err := strconv.ParseUint(port, 10, 16)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
addr[len(addr)-2], addr[len(addr)-1] = byte(portnum>>8), byte(portnum)
|
||||
|
||||
return addr
|
||||
}
|
||||
|
||||
// Handshake fast-tracks SOCKS initialization to get target address to connect.
|
||||
func Handshake(rw io.ReadWriter) (Addr, error) {
|
||||
// Read RFC 1928 for request and reply structure and sizes.
|
||||
buf := make([]byte, MaxAddrLen)
|
||||
// read VER, NMETHODS, METHODS
|
||||
if _, err := io.ReadFull(rw, buf[:2]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nmethods := buf[1]
|
||||
if _, err := io.ReadFull(rw, buf[:nmethods]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// write VER METHOD
|
||||
if _, err := rw.Write([]byte{5, 0}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// read VER CMD RSV ATYP DST.ADDR DST.PORT
|
||||
if _, err := io.ReadFull(rw, buf[:3]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cmd := buf[1]
|
||||
addr, err := readAddr(rw, buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch cmd {
|
||||
case CmdConnect:
|
||||
_, err = rw.Write([]byte{5, 0, 0, 1, 0, 0, 0, 0, 0, 0}) // SOCKS v5, reply succeeded
|
||||
case CmdUDPAssociate:
|
||||
if !UDPEnabled {
|
||||
return nil, ErrCommandNotSupported
|
||||
}
|
||||
listenAddr := ParseAddr(rw.(net.Conn).LocalAddr().String())
|
||||
_, err = rw.Write(append([]byte{5, 0, 0}, listenAddr...)) // SOCKS v5, reply succeeded
|
||||
if err != nil {
|
||||
return nil, ErrCommandNotSupported
|
||||
}
|
||||
err = InfoUDPAssociate
|
||||
default:
|
||||
return nil, ErrCommandNotSupported
|
||||
}
|
||||
|
||||
return addr, err // skip VER, CMD, RSV fields
|
||||
}
|
Reference in New Issue
Block a user