mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 02:04:01 +03:00
REPOSITORY / ScuroNeko/mtg
Compare commits
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
243a89a68c | ||
|
|
9f20e8749a | ||
|
|
b0d86abc74 | ||
|
|
6c0030d20e | ||
|
|
8733f6d191 | ||
|
|
8906ae0db0 | ||
|
|
2e8c9623de | ||
|
|
0ce325f908 | ||
|
|
b86ad07ba9 | ||
|
|
6253c21615 | ||
|
|
a00408562f | ||
|
|
0d0bdd9fde | ||
|
|
c8da08ecce | ||
|
|
c5f99d17b7 | ||
|
|
c1b862c1e9 | ||
|
|
c1e5c33076 | ||
|
|
0f732684de | ||
|
|
eb1b2ac6f1 | ||
|
|
c814e9405d | ||
|
|
0344330230 | ||
|
|
5ddf3d77d1 | ||
|
|
c64f97082c | ||
|
|
18e46241f8 | ||
|
|
c8bfa4f49f | ||
|
|
247ee5b20b | ||
|
|
71da117d94 | ||
|
|
d0ae470c73 | ||
|
|
daa2d5b901 | ||
|
|
253c705ed6 | ||
|
|
e8a3d570d1 | ||
|
|
607f4f42c4 | ||
|
|
94c2f3c215 | ||
|
|
1f74aabbe4 | ||
|
|
6c0954004e | ||
|
|
c71c90420e | ||
|
|
31f71b0e12 | ||
|
|
0b555c4deb | ||
|
|
3d8ffdcc56 | ||
|
|
25d02cf120 | ||
|
|
7a99a4ec32 |
@@ -0,0 +1,3 @@
|
|||||||
|
version.go
|
||||||
|
vendor/
|
||||||
|
tags
|
||||||
@@ -8,3 +8,4 @@
|
|||||||
mtg
|
mtg
|
||||||
vendor/
|
vendor/
|
||||||
version.go
|
version.go
|
||||||
|
ccbuilds/
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
[run]
|
||||||
|
concurrency = 4
|
||||||
|
deadline = "2m"
|
||||||
|
tests = true
|
||||||
|
skip-dirs = ["vendor"]
|
||||||
|
skip-files = ["version.go"]
|
||||||
|
|
||||||
|
[output]
|
||||||
|
format = "colored-line-number"
|
||||||
|
|
||||||
|
[linters]
|
||||||
|
enable-all = true
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
{
|
|
||||||
"aggregate": true,
|
|
||||||
"vendor": true
|
|
||||||
}
|
|
||||||
@@ -9,10 +9,18 @@ go:
|
|||||||
- "1.10.x"
|
- "1.10.x"
|
||||||
- master
|
- master
|
||||||
|
|
||||||
|
before_script: make prepare
|
||||||
|
|
||||||
script:
|
script:
|
||||||
|
- make all
|
||||||
- make lint
|
- make lint
|
||||||
|
- make critic
|
||||||
- make test
|
- make test
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
directories:
|
directories:
|
||||||
- vendor
|
- vendor
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
allow_failures:
|
||||||
|
- go: master
|
||||||
|
|||||||
+8
-2
@@ -13,11 +13,17 @@ RUN set -x \
|
|||||||
upx \
|
upx \
|
||||||
&& update-ca-certificates
|
&& update-ca-certificates
|
||||||
|
|
||||||
ADD . /go/src/github.com/9seconds/mtg
|
COPY Gopkg.toml Gopkg.lock Makefile /go/src/github.com/9seconds/mtg/
|
||||||
|
|
||||||
|
RUN set -x && \
|
||||||
|
cd /go/src/github.com/9seconds/mtg && \
|
||||||
|
make -j 4 prepare && \
|
||||||
|
make vendor
|
||||||
|
|
||||||
|
COPY . /go/src/github.com/9seconds/mtg
|
||||||
|
|
||||||
RUN set -x \
|
RUN set -x \
|
||||||
&& cd /go/src/github.com/9seconds/mtg \
|
&& cd /go/src/github.com/9seconds/mtg \
|
||||||
&& make clean \
|
|
||||||
&& make -j 4 static \
|
&& make -j 4 static \
|
||||||
&& upx --ultra-brute -qq ./mtg
|
&& upx --ultra-brute -qq ./mtg
|
||||||
|
|
||||||
|
|||||||
Generated
+70
-7
@@ -3,68 +3,97 @@
|
|||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
|
digest = "1:315c5f2f60c76d89b871c73f9bd5fe689cad96597afd50fb9992228ef80bdd34"
|
||||||
name = "github.com/alecthomas/template"
|
name = "github.com/alecthomas/template"
|
||||||
packages = [
|
packages = [
|
||||||
".",
|
".",
|
||||||
"parse"
|
"parse",
|
||||||
]
|
]
|
||||||
|
pruneopts = "UT"
|
||||||
revision = "a0175ee3bccc567396460bf5acd36800cb10c49c"
|
revision = "a0175ee3bccc567396460bf5acd36800cb10c49c"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
|
digest = "1:c198fdc381e898e8fb62b8eb62758195091c313ad18e52a3067366e1dda2fb3c"
|
||||||
name = "github.com/alecthomas/units"
|
name = "github.com/alecthomas/units"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
revision = "2efee857e7cfd4f3d0138cc3cbb1b4966962b93a"
|
revision = "2efee857e7cfd4f3d0138cc3cbb1b4966962b93a"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
|
digest = "1:a8d622a8049a4aa420e1c509873bb85d4c45c5107f420d922f919bfcb8d08694"
|
||||||
|
name = "github.com/beevik/ntp"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "62c80a04de2086884d8296004b6d74ee1846c582"
|
||||||
|
version = "v0.2.0"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:a2c1d0e43bd3baaa071d1b9ed72c27d78169b2b269f71c105ac4ba34b1be4a39"
|
||||||
name = "github.com/davecgh/go-spew"
|
name = "github.com/davecgh/go-spew"
|
||||||
packages = ["spew"]
|
packages = ["spew"]
|
||||||
|
pruneopts = "UT"
|
||||||
revision = "346938d642f2ec3594ed81d874461961cd0faa76"
|
revision = "346938d642f2ec3594ed81d874461961cd0faa76"
|
||||||
version = "v1.1.0"
|
version = "v1.1.0"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
|
digest = "1:6f9339c912bbdda81302633ad7e99a28dfa5a639c864061f1929510a9a64aa74"
|
||||||
name = "github.com/dustin/go-humanize"
|
name = "github.com/dustin/go-humanize"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
revision = "02af3965c54e8cacf948b97fef38925c4120652c"
|
pruneopts = "UT"
|
||||||
|
revision = "9f541cc9db5d55bce703bd99987c9d5cb8eea45e"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
|
digest = "1:53bd4347b151fcbedcdda527f7ebf4924f0e21d672131812f857175d8c7a1051"
|
||||||
name = "github.com/juju/errors"
|
name = "github.com/juju/errors"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
revision = "c7d06af17c68cd34c835053720b21f6549d9b0ee"
|
pruneopts = "UT"
|
||||||
|
revision = "812b06ada1776ad4dd95d575e18ffffe3a9ac34a"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
|
digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe"
|
||||||
name = "github.com/pmezard/go-difflib"
|
name = "github.com/pmezard/go-difflib"
|
||||||
packages = ["difflib"]
|
packages = ["difflib"]
|
||||||
|
pruneopts = "UT"
|
||||||
revision = "792786c7400a136282c1664665ae0a8db921c6c2"
|
revision = "792786c7400a136282c1664665ae0a8db921c6c2"
|
||||||
version = "v1.0.0"
|
version = "v1.0.0"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
|
digest = "1:274f67cb6fed9588ea2521ecdac05a6d62a8c51c074c1fccc6a49a40ba80e925"
|
||||||
name = "github.com/satori/go.uuid"
|
name = "github.com/satori/go.uuid"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
revision = "f58768cc1a7a7e77a3bd49e98cdd21419399b6a3"
|
revision = "f58768cc1a7a7e77a3bd49e98cdd21419399b6a3"
|
||||||
version = "v1.2.0"
|
version = "v1.2.0"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
|
digest = "1:18752d0b95816a1b777505a97f71c7467a8445b8ffb55631a7bf779f6ba4fa83"
|
||||||
name = "github.com/stretchr/testify"
|
name = "github.com/stretchr/testify"
|
||||||
packages = ["assert"]
|
packages = ["assert"]
|
||||||
|
pruneopts = "UT"
|
||||||
revision = "f35b8ab0b5a2cef36673838d662e249dd9c94686"
|
revision = "f35b8ab0b5a2cef36673838d662e249dd9c94686"
|
||||||
version = "v1.2.2"
|
version = "v1.2.2"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
|
digest = "1:3c1a69cdae3501bf75e76d0d86dc6f2b0a7421bc205c0cb7b96b19eed464a34d"
|
||||||
name = "go.uber.org/atomic"
|
name = "go.uber.org/atomic"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
revision = "1ea20fb1cbb1cc08cbd0d913a96dead89aa18289"
|
revision = "1ea20fb1cbb1cc08cbd0d913a96dead89aa18289"
|
||||||
version = "v1.3.2"
|
version = "v1.3.2"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
|
digest = "1:60bf2a5e347af463c42ed31a493d817f8a72f102543060ed992754e689805d1a"
|
||||||
name = "go.uber.org/multierr"
|
name = "go.uber.org/multierr"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
revision = "3c4937480c32f4c13a875a1829af76c98ca3d40a"
|
revision = "3c4937480c32f4c13a875a1829af76c98ca3d40a"
|
||||||
version = "v1.1.0"
|
version = "v1.1.0"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
|
digest = "1:d9a420eae5f76973feeb733fbf58f6a89173255b63b27a7ae4b2124a73dc6c5b"
|
||||||
name = "go.uber.org/zap"
|
name = "go.uber.org/zap"
|
||||||
packages = [
|
packages = [
|
||||||
".",
|
".",
|
||||||
@@ -72,20 +101,54 @@
|
|||||||
"internal/bufferpool",
|
"internal/bufferpool",
|
||||||
"internal/color",
|
"internal/color",
|
||||||
"internal/exit",
|
"internal/exit",
|
||||||
"zapcore"
|
"zapcore",
|
||||||
]
|
]
|
||||||
revision = "eeedf312bc6c57391d84767a4cd413f02a917974"
|
pruneopts = "UT"
|
||||||
version = "v1.8.0"
|
revision = "4d45f9617f7d90f7a663ff21c7a4321dbe78098b"
|
||||||
|
version = "v1.9.0"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
|
branch = "master"
|
||||||
|
digest = "1:becb2131aece71c64ebca5ddfd38cf631784fbb3a678d73ead3b42107c9f41ce"
|
||||||
|
name = "golang.org/x/net"
|
||||||
|
packages = [
|
||||||
|
"bpf",
|
||||||
|
"internal/iana",
|
||||||
|
"internal/socket",
|
||||||
|
"ipv4",
|
||||||
|
]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "3673e40ba22529d22c3fd7c93e97b0ce50fa7bdd"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:c06d9e11d955af78ac3bbb26bd02e01d2f61f689e1a3bce2ef6fb683ef8a7f2d"
|
||||||
name = "gopkg.in/alecthomas/kingpin.v2"
|
name = "gopkg.in/alecthomas/kingpin.v2"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
revision = "947dcec5ba9c011838740e680966fd7087a71d0d"
|
revision = "947dcec5ba9c011838740e680966fd7087a71d0d"
|
||||||
version = "v2.2.6"
|
version = "v2.2.6"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:38b469493eb173db9c03321d64adcad4c7991ea0a19b5edc5bdc094f0e8c7384"
|
||||||
|
name = "gopkg.in/alexcesaro/statsd.v2"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "7fea3f0d2fab1ad973e641e51dba45443a311a90"
|
||||||
|
version = "v2.0.0"
|
||||||
|
|
||||||
[solve-meta]
|
[solve-meta]
|
||||||
analyzer-name = "dep"
|
analyzer-name = "dep"
|
||||||
analyzer-version = 1
|
analyzer-version = 1
|
||||||
inputs-digest = "c4fdd3664f683342ad0c2509f4a8bcfe5b267a6e8cdaf36f70d39536bbf89834"
|
input-imports = [
|
||||||
|
"github.com/beevik/ntp",
|
||||||
|
"github.com/dustin/go-humanize",
|
||||||
|
"github.com/juju/errors",
|
||||||
|
"github.com/satori/go.uuid",
|
||||||
|
"github.com/stretchr/testify/assert",
|
||||||
|
"go.uber.org/zap",
|
||||||
|
"go.uber.org/zap/zapcore",
|
||||||
|
"gopkg.in/alecthomas/kingpin.v2",
|
||||||
|
"gopkg.in/alexcesaro/statsd.v2",
|
||||||
|
]
|
||||||
solver-name = "gps-cdcl"
|
solver-name = "gps-cdcl"
|
||||||
solver-version = 1
|
solver-version = 1
|
||||||
|
|||||||
@@ -48,3 +48,11 @@
|
|||||||
[[constraint]]
|
[[constraint]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
name = "github.com/dustin/go-humanize"
|
name = "github.com/dustin/go-humanize"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/beevik/ntp"
|
||||||
|
version = "0.2.0"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "gopkg.in/alexcesaro/statsd.v2"
|
||||||
|
version = "2.0.0"
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
|
ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
|
||||||
IMAGE_NAME := mtg
|
IMAGE_NAME := mtg
|
||||||
APP_NAME := $(IMAGE_NAME)
|
APP_NAME := $(IMAGE_NAME)
|
||||||
GOMETALINTER := gometalinter
|
|
||||||
|
|
||||||
VENDOR_FILES := $(shell find "$(ROOT_DIR)/vendor" 2>/dev/null || echo -n "vendor")
|
VENDOR_FILES := $(shell find "$(ROOT_DIR)/vendor" 2>/dev/null || echo -n "vendor")
|
||||||
CC_BINARIES := $(shell bash -c "echo -n $(APP_NAME)-{linux,windows,darwin,freebsd,openbsd}-{386,amd64} $(APP_NAME)-linux-{arm,arm64}")
|
CC_BINARIES := $(shell bash -c "echo -n $(APP_NAME)-{linux,freebsd,openbsd}-{386,amd64} $(APP_NAME)-linux-{arm,arm64}")
|
||||||
APP_DEPS := version.go $(VENDOR_FILES)
|
APP_DEPS := version.go $(VENDOR_FILES)
|
||||||
|
|
||||||
|
GOLANGCI_LINT_VERSION := v1.9.1
|
||||||
|
|
||||||
COMMON_BUILD_FLAGS := -ldflags="-s -w"
|
COMMON_BUILD_FLAGS := -ldflags="-s -w"
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
@@ -31,8 +32,8 @@ ccbuilds:
|
|||||||
version.go:
|
version.go:
|
||||||
@go generate main.go
|
@go generate main.go
|
||||||
|
|
||||||
vendor: Gopkg.lock Gopkg.toml install-cli
|
vendor: Gopkg.lock Gopkg.toml
|
||||||
@dep ensure
|
@dep ensure --vendor-only
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
@@ -50,12 +51,16 @@ crosscompile-dir:
|
|||||||
@rm -rf "$(CC_DIR)" && mkdir -p "$(CC_DIR)"
|
@rm -rf "$(CC_DIR)" && mkdir -p "$(CC_DIR)"
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: vendor install-cli version.go
|
test: vendor version.go
|
||||||
@go test -v ./...
|
@go test -v ./...
|
||||||
|
|
||||||
.PHONY: lint
|
.PHONY: lint
|
||||||
lint: vendor install-cli version.go
|
lint: version.go
|
||||||
@$(GOMETALINTER) --deadline=2m ./...
|
@golangci-lint run
|
||||||
|
|
||||||
|
.PHONY: critic
|
||||||
|
critic: version.go
|
||||||
|
@gocritic check-project "$(ROOT_DIR)"
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
@@ -67,14 +72,18 @@ clean:
|
|||||||
docker:
|
docker:
|
||||||
@docker build --pull -t "$(IMAGE_NAME)" "$(ROOT_DIR)"
|
@docker build --pull -t "$(IMAGE_NAME)" "$(ROOT_DIR)"
|
||||||
|
|
||||||
.PHONY: install-cli
|
.PHONY: prepare
|
||||||
install-cli: install-dep install-lint
|
prepare: install-dep install-lint install-critic
|
||||||
|
|
||||||
.PHONY: install-dep
|
.PHONY: install-dep
|
||||||
install-dep:
|
install-dep:
|
||||||
@go get github.com/golang/dep/cmd/dep
|
@go get -u github.com/golang/dep/cmd/dep
|
||||||
|
|
||||||
.PHONY: install-lint
|
.PHONY: install-lint
|
||||||
install-lint:
|
install-lint:
|
||||||
@go get github.com/alecthomas/gometalinter && \
|
@curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh \
|
||||||
$(GOMETALINTER) --install >/dev/null
|
| bash -s -- -b $(GOPATH)/bin $(GOLANGCI_LINT_VERSION)
|
||||||
|
|
||||||
|
.PHONY: install-critic
|
||||||
|
install-critic:
|
||||||
|
@go get -u github.com/go-critic/go-critic/...
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ Bullshit-free MTPROTO proxy for Telegram
|
|||||||
[](https://travis-ci.org/9seconds/mtg)
|
[](https://travis-ci.org/9seconds/mtg)
|
||||||
[](https://hub.docker.com/r/nineseconds/mtg/)
|
[](https://hub.docker.com/r/nineseconds/mtg/)
|
||||||
|
|
||||||
|
|
||||||
# Rationale
|
# Rationale
|
||||||
|
|
||||||
There are several available proxies for Telegram MTPROTO available. Here
|
There are several available proxies for Telegram MTPROTO available. Here
|
||||||
@@ -14,7 +13,6 @@ are the most notable:
|
|||||||
* [Official](https://github.com/TelegramMessenger/MTProxy)
|
* [Official](https://github.com/TelegramMessenger/MTProxy)
|
||||||
* [Python](https://github.com/alexbers/mtprotoproxy)
|
* [Python](https://github.com/alexbers/mtprotoproxy)
|
||||||
* [Erlang](https://github.com/seriyps/mtproto_proxy)
|
* [Erlang](https://github.com/seriyps/mtproto_proxy)
|
||||||
* [JS](https://github.com/FreedomPrevails/JSMTProxy)
|
|
||||||
|
|
||||||
Almost all of them follow the way how official proxy was build. This
|
Almost all of them follow the way how official proxy was build. This
|
||||||
includes support of multiple secrets, support of promoted channels etc.
|
includes support of multiple secrets, support of promoted channels etc.
|
||||||
@@ -34,7 +32,7 @@ mtg is an implementation in golang which is intended to be:
|
|||||||
software. I also believe that in case of throwout proxies, this feature
|
software. I also believe that in case of throwout proxies, this feature
|
||||||
is useless luxury.
|
is useless luxury.
|
||||||
* **Minimum docker image size**
|
* **Minimum docker image size**
|
||||||
Official image is less than 2 megabytes. Literally.
|
Official image is less than 2.5 megabytes. Literally.
|
||||||
* **No management WebUI**
|
* **No management WebUI**
|
||||||
This is an implementation of simple lightweight proxy. I won't do that.
|
This is an implementation of simple lightweight proxy. I won't do that.
|
||||||
|
|
||||||
@@ -51,28 +49,49 @@ To get promoted channel, please contact
|
|||||||
second parameter.
|
second parameter.
|
||||||
|
|
||||||
|
|
||||||
|
# Source code organization
|
||||||
|
|
||||||
|
There are 2 main branches:
|
||||||
|
|
||||||
|
1. `master` branch contains potentially unstable features
|
||||||
|
2. `stable` branch contains stable version. Usually you want to use this branch.
|
||||||
|
|
||||||
# How to build
|
# How to build
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ make
|
make
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want to build for another platform:
|
If you want to build for another platform:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ make crosscompile
|
make crosscompile
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want to build Docker image (called `mtg`):
|
If you want to build Docker image (called `mtg`):
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ make docker
|
make docker
|
||||||
```
|
```
|
||||||
|
|
||||||
# Docker image
|
# Docker image
|
||||||
|
|
||||||
|
Docker follows the same policy as the source code organization:
|
||||||
|
|
||||||
|
- `latest` mirrors the master branch
|
||||||
|
- `stable` mirrors the stable branch
|
||||||
|
- tags are for tagged releases
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ docker pull nineseconds/mtg
|
docker pull nineseconds/mtg:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
```console
|
||||||
|
docker pull nineseconds/mtg:stable
|
||||||
|
```
|
||||||
|
|
||||||
|
```console
|
||||||
|
docker pull nineseconds/mtg:0.10
|
||||||
```
|
```
|
||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
@@ -82,27 +101,96 @@ Basically, to run this tool you need to configure as less as possible.
|
|||||||
First, you need to generate a secret:
|
First, you need to generate a secret:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ openssl rand -hex 16
|
openssl rand -hex 16
|
||||||
```
|
```
|
||||||
|
|
||||||
or
|
or
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ head -c 512 /dev/urandom | md5sum | cut -f 1 -d ' '
|
head -c 512 /dev/urandom | md5sum | cut -f 1 -d ' '
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Secure mode
|
||||||
|
|
||||||
|
If you want to support new secure mode, please prepend `dd` to the
|
||||||
|
secret. For example, secret `cf18fa8ea0267057e2c61a5f7322a8e7` should
|
||||||
|
be `ddcf18fa8ea0267057e2c61a5f7322a8e7`. But pay attention that some
|
||||||
|
old clients won't support this mode. If this is not your case, I would
|
||||||
|
suggest to go with this mode.
|
||||||
|
|
||||||
|
Oneliners to generate such secrets:
|
||||||
|
|
||||||
|
```console
|
||||||
|
echo dd$(openssl rand -hex 16)
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```console
|
||||||
|
echo dd$(head -c 512 /dev/urandom | md5sum | cut -f 1 -d ' ')
|
||||||
|
```
|
||||||
|
|
||||||
|
## Environment variables
|
||||||
|
|
||||||
|
It is possible to configure this tool using environment variables. You
|
||||||
|
can configure any flag but not secret or adtag. Here is the list of
|
||||||
|
supported environment variables:
|
||||||
|
|
||||||
|
| Environment variable | Corresponding flags | Default value | Description |
|
||||||
|
|--------------------------|------------------------|-----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||||
|
| `MTG_DEBUG` | `-d`, `--debug` | `false` | Run in debug mode. Usually, you need to run in this mode only if you develop this tool or its maintainer is asking you to provide logs with such verbosity. |
|
||||||
|
| `MTG_VERBOSE` | `-v`, `--verbose` | `false` | Run in verbose mode. This is way less chatty than debug mode. |
|
||||||
|
| `MTG_IP` | `-b`, `--bind-ip` | `127.0.0.1` | Which IP should we bind to. As usual, `0.0.0.0` means that we want to listen on all interfaces. Also, 4 zeroes will bind to both IPv4 and IPv6. |
|
||||||
|
| `MTG_PORT` | `-p`, `--bind-port` | `3128` | Which port should we bind to (listen on). |
|
||||||
|
| `MTG_IPV4` | `-4`, `--public-ipv4` | [Autodetect](https://ifconfig.co) | IPv4 address of this proxy. This is required if you NAT your proxy or run it in a docker container. In that case, you absolutely need to specify public IPv4 address of the proxy, otherwise either URLs will be broken or proxy could not access Telegram middle proxies. |
|
||||||
|
| `MTG_IPV4_PORT` | `--public-ipv4-port` | Value of `--bind-port` | Which port should be public of IPv4 interface. This affects only generated links and should be changed only if you NAT your proxy or run it in a docker container. |
|
||||||
|
| `MTG_IPV6` | `-6`, `--public-ipv6` | [Autodetect](https://ifconfig.co) | IPv6 address of this proxy. This is required if you NAT your proxy or run it in a docker container. In that case, you absolutely need to specify public IPv6 address of the proxy, otherwise either URLs will be broken or proxy could not access Telegram middle proxies. |
|
||||||
|
| `MTG_IPV6_PORT` | `--public-ipv6-port` | Value of `--bind-port` | Which port should be public of IPv6 interface. This affects only generated links and should be changed only if you NAT your proxy or run it in a docker container. |
|
||||||
|
| `MTG_STATS_IP` | `-t`, `--stats-ip` | `127.0.0.1` | Which IP should we bind the internal statistics HTTP server. |
|
||||||
|
| `MTG_STATS_PORT` | `-q`, `--stats-port` | `3129` | Which port should we bind the internal statistics HTTP server. |
|
||||||
|
| `MTG_STATSD_IP` | `--statsd-ip` | | IP/host addresses of statsd service. No defaults, by defaults we do not send anything there. |
|
||||||
|
| `MTG_STATSD_PORT` | `--statsd-port` | `8125` | Which port should we use to work with statsd. |
|
||||||
|
| `MTG_STATSD_NETWORK` | `--statsd-network` | `udp` | Which protocol should we use to work with statsd. Possible options are `udp` and `tcp`. |
|
||||||
|
| `MTG_STATSD_PREFIX` | `--statsd-prefix` | `mtg` | Which bucket prefix we should use. For example, if you set `mtg`, then metric `traffic.ingress` would be send as `mtg.traffic.ingress`. |
|
||||||
|
| `MTG_STATSD_TAGS_FORMAT` | `--statsd-tags-format` | | Which tags format we should use. By default, we are using default vanilla statsd tags format but if you want to send directly to InfluxDB or Datadog, please specify it there. Possible options are `influxdb` and `datadog`. |
|
||||||
|
| `MTG_STATSD_TAGS` | `--statsd-tags` | | Which tags should we send to statsd with our metrics. Please specify them as `key=value` pairs. |
|
||||||
|
| `MTG_BUFFER_WRITE` | `-w`, `--write-buffer` | `65536` | The size of TCP write buffer in bytes. Write buffer is the buffer for messages which are going from client to Telegram. |
|
||||||
|
| `MTG_BUFFER_READ` | `-r`, `--read-buffer` | `131072` | The size of TCP read buffer in bytes. Read buffer is the buffer for messages from Telegram to client. |
|
||||||
|
|
||||||
|
Usually you want to modify only read/write buffer sizes. If you feel
|
||||||
|
that proxy is slow, try to increase both sizes giving more priority to
|
||||||
|
read buffer.
|
||||||
|
|
||||||
|
Unfortunately, MTPROTO proxy protocol does not allow us to use splice
|
||||||
|
or any other neat tricks how to eliminate the need of copying data into
|
||||||
|
userspace.
|
||||||
|
|
||||||
|
# How to run the tool
|
||||||
|
|
||||||
Now run the tool:
|
Now run the tool:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ mtg <secret>
|
mtg <secret>
|
||||||
|
```
|
||||||
|
|
||||||
|
How to run the tool with ADTag:
|
||||||
|
|
||||||
|
```console
|
||||||
|
mtg <secret> <adtag>
|
||||||
```
|
```
|
||||||
|
|
||||||
This tool will listen on port 3128 by default with the given secret.
|
This tool will listen on port 3128 by default with the given secret.
|
||||||
|
|
||||||
# One-line runner
|
# One-line runner
|
||||||
|
|
||||||
|
```console
|
||||||
|
docker run --name mtg --restart=unless-stopped -p 3128:3128 -p 3129:3129 -d nineseconds/mtg:stable $(openssl rand -hex 16)
|
||||||
```
|
```
|
||||||
$ docker run --name mtg --restart=unless-stopped -p 3128:3128 -p 3129:3129 -d nineseconds/mtg $(openssl rand -hex 16)
|
|
||||||
|
or in secret mode:
|
||||||
|
|
||||||
|
```console
|
||||||
|
docker run --name mtg --restart=unless-stopped -p 3128:3128 -p 3129:3129 -d nineseconds/mtg:stable dd$(openssl rand -hex 16)
|
||||||
```
|
```
|
||||||
|
|
||||||
You will have this tool up and running on port 3128. Now curl
|
You will have this tool up and running on port 3128. Now curl
|
||||||
@@ -110,3 +198,33 @@ You will have this tool up and running on port 3128. Now curl
|
|||||||
port 3129 will show you some statistics if you are interested in.
|
port 3129 will show you some statistics if you are interested in.
|
||||||
|
|
||||||
Also, you can use [run-mtg.sh](https://github.com/9seconds/mtg/blob/master/run-mtg.sh) script
|
Also, you can use [run-mtg.sh](https://github.com/9seconds/mtg/blob/master/run-mtg.sh) script
|
||||||
|
|
||||||
|
|
||||||
|
# statsd integration
|
||||||
|
|
||||||
|
mtg provides an integration with statsd, you can enable it with command
|
||||||
|
line interface. To enable it, you have to provide IP address of statsd
|
||||||
|
service.
|
||||||
|
|
||||||
|
Out of the box, mtg supports 2 additional dialects: [InfluxDB](https://www.influxdata.com/blog/getting-started-with-sending-statsd-metrics-to-telegraf-influxdb/)
|
||||||
|
and [Datadog](https://docs.datadoghq.com/developers/dogstatsd/).
|
||||||
|
|
||||||
|
All metrics are gauges. Here is the list of metrics and their meaning:
|
||||||
|
|
||||||
|
| Metric name | Unit | Description |
|
||||||
|
|---------------------------------|---------|-----------------------------------------------------------|
|
||||||
|
| `connections.abridged.ipv4` | number | The number of active abridged IPv4 connections |
|
||||||
|
| `connections.abridged.ipv6` | number | The number of active abridged IPv6 connections |
|
||||||
|
| `connections.intermediate.ipv4` | number | The number of active intermediate IPv4 connections |
|
||||||
|
| `connections.intermediate.ipv6` | number | The number of active intermediate IPv6 connections |
|
||||||
|
| `connections.secure.ipv4` | number | The number of active secure intermediate IPv4 connections |
|
||||||
|
| `connections.secure.ipv6` | number | The number of active secure intermediate IPv6 connections |
|
||||||
|
| `crashes` | number | An amount of crashes in client handlers |
|
||||||
|
| `traffic.ingress` | bytes | Ingress traffic from the start of application (incoming) |
|
||||||
|
| `traffic.egress` | bytes | Egress traffic from the start of application (outgoing) |
|
||||||
|
| `speed.ingress` | bytes/s | Ingress bandwidth of the latest second (incoming traffic) |
|
||||||
|
| `speed.egress` | bytes/s | Egress bandwidth of the latest second (outgoing traffic) |
|
||||||
|
|
||||||
|
All metrics are prefixed with given prefix. Default prefix is `mtg`.
|
||||||
|
With such prefix metric name `traffic.ingress`, for example, would be
|
||||||
|
`mtg.traffic.ingress`.
|
||||||
|
|||||||
+3
-1
@@ -1,6 +1,7 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/config"
|
"github.com/9seconds/mtg/config"
|
||||||
@@ -9,4 +10,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Init defines common method for initializing client connections.
|
// Init defines common method for initializing client connections.
|
||||||
type Init func(net.Conn, string, *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error)
|
type Init func(context.Context, context.CancelFunc, net.Conn, string,
|
||||||
|
*config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error)
|
||||||
|
|||||||
+7
-9
@@ -1,6 +1,7 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -12,23 +13,20 @@ import (
|
|||||||
"github.com/9seconds/mtg/wrappers"
|
"github.com/9seconds/mtg/wrappers"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const handshakeTimeout = 10 * time.Second
|
||||||
handshakeTimeout = 10 * time.Second
|
|
||||||
readBufferSize = 64 * 1024
|
|
||||||
writeBufferSize = 64 * 1024
|
|
||||||
)
|
|
||||||
|
|
||||||
// DirectInit initializes client connection for proxy which connects to
|
// DirectInit initializes client connection for proxy which connects to
|
||||||
// Telegram directly.
|
// Telegram directly.
|
||||||
func DirectInit(socket net.Conn, connID string, conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
|
func DirectInit(ctx context.Context, cancel context.CancelFunc, socket net.Conn,
|
||||||
|
connID string, conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
|
||||||
tcpSocket := socket.(*net.TCPConn)
|
tcpSocket := socket.(*net.TCPConn)
|
||||||
if err := tcpSocket.SetNoDelay(false); err != nil {
|
if err := tcpSocket.SetNoDelay(false); err != nil {
|
||||||
return nil, nil, errors.Annotate(err, "Cannot disable NO_DELAY to client socket")
|
return nil, nil, errors.Annotate(err, "Cannot disable NO_DELAY to client socket")
|
||||||
}
|
}
|
||||||
if err := tcpSocket.SetReadBuffer(readBufferSize); err != nil {
|
if err := tcpSocket.SetReadBuffer(conf.ReadBufferSize); err != nil {
|
||||||
return nil, nil, errors.Annotate(err, "Cannot set read buffer size of client socket")
|
return nil, nil, errors.Annotate(err, "Cannot set read buffer size of client socket")
|
||||||
}
|
}
|
||||||
if err := tcpSocket.SetWriteBuffer(writeBufferSize); err != nil {
|
if err := tcpSocket.SetWriteBuffer(conf.WriteBufferSize); err != nil {
|
||||||
return nil, nil, errors.Annotate(err, "Cannot set write buffer size of client socket")
|
return nil, nil, errors.Annotate(err, "Cannot set write buffer size of client socket")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +37,7 @@ func DirectInit(socket net.Conn, connID string, conf *config.Config) (wrappers.W
|
|||||||
}
|
}
|
||||||
socket.SetReadDeadline(time.Time{}) // nolint: errcheck
|
socket.SetReadDeadline(time.Time{}) // nolint: errcheck
|
||||||
|
|
||||||
conn := wrappers.NewConn(socket, connID, wrappers.ConnPurposeClient, conf.PublicIPv4, conf.PublicIPv6)
|
conn := wrappers.NewConn(ctx, cancel, socket, connID, wrappers.ConnPurposeClient, conf.PublicIPv4, conf.PublicIPv6)
|
||||||
obfs2, connOpts, err := obfuscated2.ParseObfuscated2ClientFrame(conf.Secret, frame)
|
obfs2, connOpts, err := obfuscated2.ParseObfuscated2ClientFrame(conf.Secret, frame)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
|
return nil, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
|
||||||
|
|||||||
+13
-4
@@ -1,6 +1,7 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/config"
|
"github.com/9seconds/mtg/config"
|
||||||
@@ -10,16 +11,24 @@ import (
|
|||||||
|
|
||||||
// MiddleInit initializes client connection for proxy which has to
|
// MiddleInit initializes client connection for proxy which has to
|
||||||
// support promoted channels, connect to Telegram middle proxies etc.
|
// support promoted channels, connect to Telegram middle proxies etc.
|
||||||
func MiddleInit(socket net.Conn, connID string, conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
|
func MiddleInit(ctx context.Context, cancel context.CancelFunc, socket net.Conn,
|
||||||
conn, opts, err := DirectInit(socket, connID, conf)
|
connID string, conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
|
||||||
|
conn, opts, err := DirectInit(ctx, cancel, socket, connID, conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
connStream := conn.(wrappers.StreamReadWriteCloser)
|
connStream := conn.(wrappers.StreamReadWriteCloser)
|
||||||
|
|
||||||
newConn := wrappers.NewMTProtoAbridged(connStream, opts)
|
var newConn wrappers.PacketReadWriteCloser
|
||||||
if opts.ConnectionType != mtproto.ConnectionTypeAbridged {
|
switch opts.ConnectionType {
|
||||||
|
case mtproto.ConnectionTypeAbridged:
|
||||||
|
newConn = wrappers.NewMTProtoAbridged(connStream, opts)
|
||||||
|
case mtproto.ConnectionTypeIntermediate:
|
||||||
newConn = wrappers.NewMTProtoIntermediate(connStream, opts)
|
newConn = wrappers.NewMTProtoIntermediate(connStream, opts)
|
||||||
|
case mtproto.ConnectionTypeSecure:
|
||||||
|
newConn = wrappers.NewMTProtoIntermediateSecure(connStream, opts)
|
||||||
|
default:
|
||||||
|
panic("Unknown connection type")
|
||||||
}
|
}
|
||||||
|
|
||||||
opts.ConnectionProto = mtproto.ConnectionProtocolIPv4
|
opts.ConnectionProto = mtproto.ConnectionProtocolIPv4
|
||||||
|
|||||||
+88
-31
@@ -1,24 +1,24 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
)
|
statsd "gopkg.in/alexcesaro/statsd.v2"
|
||||||
|
|
||||||
// Buffer sizes define internal socket buffer sizes.
|
|
||||||
const (
|
|
||||||
BufferWriteSize = 32 * 1024
|
|
||||||
BufferReadSize = 32 * 1024
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config represents common configuration of mtg.
|
// Config represents common configuration of mtg.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Debug bool
|
Debug bool
|
||||||
Verbose bool
|
Verbose bool
|
||||||
|
SecureMode bool
|
||||||
|
|
||||||
|
ReadBufferSize int
|
||||||
|
WriteBufferSize int
|
||||||
|
|
||||||
BindPort uint16
|
BindPort uint16
|
||||||
PublicIPv4Port uint16
|
PublicIPv4Port uint16
|
||||||
@@ -30,6 +30,14 @@ type Config struct {
|
|||||||
PublicIPv6 net.IP
|
PublicIPv6 net.IP
|
||||||
StatsIP net.IP
|
StatsIP net.IP
|
||||||
|
|
||||||
|
StatsD struct {
|
||||||
|
Addr net.Addr
|
||||||
|
Prefix string
|
||||||
|
Tags map[string]string
|
||||||
|
TagsFormat statsd.TagFormat
|
||||||
|
Enabled bool
|
||||||
|
}
|
||||||
|
|
||||||
Secret []byte
|
Secret []byte
|
||||||
AdTag []byte
|
AdTag []byte
|
||||||
}
|
}
|
||||||
@@ -46,6 +54,7 @@ type URLs struct {
|
|||||||
type IPURLs struct {
|
type IPURLs struct {
|
||||||
IPv4 URLs `json:"ipv4"`
|
IPv4 URLs `json:"ipv4"`
|
||||||
IPv6 URLs `json:"ipv6"`
|
IPv6 URLs `json:"ipv6"`
|
||||||
|
BotSecret string `json:"secret_for_mtproxybot"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// BindAddr returns connection for this server to bind to.
|
// BindAddr returns connection for this server to bind to.
|
||||||
@@ -64,15 +73,32 @@ func (c *Config) UseMiddleProxy() bool {
|
|||||||
return len(c.AdTag) > 0
|
return len(c.AdTag) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BotSecretString returns secret string which should work with MTProxybot.
|
||||||
|
func (c *Config) BotSecretString() string {
|
||||||
|
return hex.EncodeToString(c.Secret)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SecretString returns a secret in a form entered on the start of the
|
||||||
|
// application.
|
||||||
|
func (c *Config) SecretString() string {
|
||||||
|
secret := c.BotSecretString()
|
||||||
|
if c.SecureMode {
|
||||||
|
return "dd" + secret
|
||||||
|
}
|
||||||
|
return secret
|
||||||
|
}
|
||||||
|
|
||||||
// GetURLs returns configured IPURLs instance with links to this server.
|
// GetURLs returns configured IPURLs instance with links to this server.
|
||||||
func (c *Config) GetURLs() IPURLs {
|
func (c *Config) GetURLs() IPURLs {
|
||||||
urls := IPURLs{}
|
urls := IPURLs{}
|
||||||
|
secret := c.SecretString()
|
||||||
if c.PublicIPv4 != nil {
|
if c.PublicIPv4 != nil {
|
||||||
urls.IPv4 = getURLs(c.PublicIPv4, c.PublicIPv4Port, c.Secret)
|
urls.IPv4 = getURLs(c.PublicIPv4, c.PublicIPv4Port, secret)
|
||||||
}
|
}
|
||||||
if c.PublicIPv6 != nil {
|
if c.PublicIPv6 != nil {
|
||||||
urls.IPv6 = getURLs(c.PublicIPv6, c.PublicIPv6Port, c.Secret)
|
urls.IPv6 = getURLs(c.PublicIPv6, c.PublicIPv6Port, secret)
|
||||||
}
|
}
|
||||||
|
urls.BotSecret = c.BotSecretString()
|
||||||
|
|
||||||
return urls
|
return urls
|
||||||
}
|
}
|
||||||
@@ -85,27 +111,21 @@ func getAddr(host fmt.Stringer, port uint16) string {
|
|||||||
// fetches data from external sources. Parameters passed to this
|
// fetches data from external sources. Parameters passed to this
|
||||||
// function, should come from command line arguments.
|
// function, should come from command line arguments.
|
||||||
func NewConfig(debug, verbose bool, // nolint: gocyclo
|
func NewConfig(debug, verbose bool, // nolint: gocyclo
|
||||||
bindIP net.IP, bindPort uint16,
|
writeBufferSize, readBufferSize uint32,
|
||||||
publicIPv4 net.IP, PublicIPv4Port uint16,
|
bindIP, publicIPv4, publicIPv6, statsIP net.IP,
|
||||||
publicIPv6 net.IP, publicIPv6Port uint16,
|
bindPort, publicIPv4Port, publicIPv6Port, statsPort, statsdPort uint16,
|
||||||
statsIP net.IP, statsPort uint16,
|
statsdIP, statsdNetwork, statsdPrefix, statsdTagsFormat string,
|
||||||
secret, adtag string) (*Config, error) {
|
statsdTags map[string]string,
|
||||||
if len(secret) != 32 {
|
secret, adtag []byte) (*Config, error) {
|
||||||
|
secureMode := false
|
||||||
|
if bytes.HasPrefix(secret, []byte{0xdd}) && len(secret) == 17 {
|
||||||
|
secureMode = true
|
||||||
|
secret = bytes.TrimPrefix(secret, []byte{0xdd})
|
||||||
|
} else if len(secret) != 16 {
|
||||||
return nil, errors.New("Telegram demands secret of length 32")
|
return nil, errors.New("Telegram demands secret of length 32")
|
||||||
}
|
}
|
||||||
secretBytes, err := hex.DecodeString(secret)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Annotate(err, "Cannot create config")
|
|
||||||
}
|
|
||||||
|
|
||||||
var adTagBytes []byte
|
|
||||||
if len(adtag) != 0 {
|
|
||||||
adTagBytes, err = hex.DecodeString(adtag)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Annotate(err, "Cannot create config")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
var err error
|
||||||
if publicIPv4 == nil {
|
if publicIPv4 == nil {
|
||||||
publicIPv4, err = getGlobalIPv4()
|
publicIPv4, err = getGlobalIPv4()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -114,8 +134,8 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo
|
|||||||
return nil, errors.Errorf("IP %s is not IPv4", publicIPv4.String())
|
return nil, errors.Errorf("IP %s is not IPv4", publicIPv4.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if PublicIPv4Port == 0 {
|
if publicIPv4Port == 0 {
|
||||||
PublicIPv4Port = bindPort
|
publicIPv4Port = bindPort
|
||||||
}
|
}
|
||||||
|
|
||||||
if publicIPv6 == nil {
|
if publicIPv6 == nil {
|
||||||
@@ -140,13 +160,50 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo
|
|||||||
BindIP: bindIP,
|
BindIP: bindIP,
|
||||||
BindPort: bindPort,
|
BindPort: bindPort,
|
||||||
PublicIPv4: publicIPv4,
|
PublicIPv4: publicIPv4,
|
||||||
PublicIPv4Port: PublicIPv4Port,
|
PublicIPv4Port: publicIPv4Port,
|
||||||
PublicIPv6: publicIPv6,
|
PublicIPv6: publicIPv6,
|
||||||
PublicIPv6Port: publicIPv6Port,
|
PublicIPv6Port: publicIPv6Port,
|
||||||
StatsIP: statsIP,
|
StatsIP: statsIP,
|
||||||
StatsPort: statsPort,
|
StatsPort: statsPort,
|
||||||
Secret: secretBytes,
|
Secret: secret,
|
||||||
AdTag: adTagBytes,
|
AdTag: adtag,
|
||||||
|
SecureMode: secureMode,
|
||||||
|
ReadBufferSize: int(readBufferSize),
|
||||||
|
WriteBufferSize: int(writeBufferSize),
|
||||||
|
}
|
||||||
|
|
||||||
|
if statsdIP != "" {
|
||||||
|
conf.StatsD.Enabled = true
|
||||||
|
conf.StatsD.Prefix = statsdPrefix
|
||||||
|
conf.StatsD.Tags = statsdTags
|
||||||
|
|
||||||
|
var (
|
||||||
|
addr net.Addr
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
hostPort := net.JoinHostPort(statsdIP, strconv.Itoa(int(statsdPort)))
|
||||||
|
switch statsdNetwork {
|
||||||
|
case "tcp":
|
||||||
|
addr, err = net.ResolveTCPAddr("tcp", hostPort)
|
||||||
|
case "udp":
|
||||||
|
addr, err = net.ResolveUDPAddr("udp", hostPort)
|
||||||
|
default:
|
||||||
|
err = errors.Errorf("Unknown network %s", statsdNetwork)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Annotate(err, "Cannot resolve statsd address")
|
||||||
|
}
|
||||||
|
conf.StatsD.Addr = addr
|
||||||
|
|
||||||
|
switch statsdTagsFormat {
|
||||||
|
case "datadog":
|
||||||
|
conf.StatsD.TagsFormat = statsd.Datadog
|
||||||
|
case "influxdb":
|
||||||
|
conf.StatsD.TagsFormat = statsd.InfluxDB
|
||||||
|
case "":
|
||||||
|
default:
|
||||||
|
return nil, errors.Errorf("Unknown tags format %s", statsdTagsFormat)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return conf, nil
|
return conf, nil
|
||||||
|
|||||||
+17
-4
@@ -1,6 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -9,16 +10,28 @@ import (
|
|||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const ifconfigAddress = "https://ifconfig.co/ip"
|
||||||
|
|
||||||
func getGlobalIPv4() (net.IP, error) {
|
func getGlobalIPv4() (net.IP, error) {
|
||||||
return fetchIP("https://v4.ifconfig.co/ip")
|
return fetchIP("tcp4")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getGlobalIPv6() (net.IP, error) {
|
func getGlobalIPv6() (net.IP, error) {
|
||||||
return fetchIP("https://v6.ifconfig.co/ip")
|
return fetchIP("tcp6")
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchIP(url string) (net.IP, error) {
|
func fetchIP(network string) (net.IP, error) {
|
||||||
resp, err := http.Get(url)
|
dialer := &net.Dialer{DualStack: false}
|
||||||
|
client := &http.Client{
|
||||||
|
Jar: nil,
|
||||||
|
Transport: &http.Transport{
|
||||||
|
DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) {
|
||||||
|
return dialer.DialContext(ctx, network, addr)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Get(ifconfigAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-6
@@ -1,17 +1,16 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getURLs(addr net.IP, port uint16, secret []byte) (urls URLs) {
|
func getURLs(addr net.IP, port uint16, secret string) (urls URLs) {
|
||||||
values := url.Values{}
|
values := url.Values{}
|
||||||
values.Set("server", addr.String())
|
values.Set("server", addr.String())
|
||||||
values.Set("port", strconv.Itoa(int(port)))
|
values.Set("port", strconv.Itoa(int(port)))
|
||||||
values.Set("secret", hex.EncodeToString(secret))
|
values.Set("secret", secret)
|
||||||
|
|
||||||
urls.TG = makeTGURL(values)
|
urls.TG = makeTGURL(values)
|
||||||
urls.TMe = makeTMeURL(values)
|
urls.TMe = makeTMeURL(values)
|
||||||
@@ -43,7 +42,7 @@ func makeTMeURL(values url.Values) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func makeQRCodeURL(data string) string {
|
func makeQRCodeURL(data string) string {
|
||||||
QRURL := url.URL{
|
qr := url.URL{
|
||||||
Scheme: "https",
|
Scheme: "https",
|
||||||
Host: "api.qrserver.com",
|
Host: "api.qrserver.com",
|
||||||
Path: "v1/create-qr-code",
|
Path: "v1/create-qr-code",
|
||||||
@@ -53,7 +52,7 @@ func makeQRCodeURL(data string) string {
|
|||||||
values.Set("qzone", "4")
|
values.Set("qzone", "4")
|
||||||
values.Set("format", "svg")
|
values.Set("format", "svg")
|
||||||
values.Set("data", data)
|
values.Set("data", data)
|
||||||
QRURL.RawQuery = values.Encode()
|
qr.RawQuery = values.Encode()
|
||||||
|
|
||||||
return QRURL.String()
|
return qr.String()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,83 +4,136 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/juju/errors"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/config"
|
"github.com/9seconds/mtg/config"
|
||||||
|
"github.com/9seconds/mtg/ntp"
|
||||||
"github.com/9seconds/mtg/proxy"
|
"github.com/9seconds/mtg/proxy"
|
||||||
"github.com/9seconds/mtg/stats"
|
"github.com/9seconds/mtg/stats"
|
||||||
"github.com/juju/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
app = kingpin.New("mtg", "Simple MTPROTO proxy.")
|
app = kingpin.New("mtg", "Simple MTPROTO proxy.")
|
||||||
|
|
||||||
debug = app.Flag("debug", "Run in debug mode.").
|
debug = app.Flag("debug",
|
||||||
|
"Run in debug mode.").
|
||||||
Short('d').
|
Short('d').
|
||||||
Envar("MTG_DEBUG").
|
Envar("MTG_DEBUG").
|
||||||
Bool()
|
Bool()
|
||||||
verbose = app.Flag("verbose", "Run in verbose mode.").
|
verbose = app.Flag("verbose",
|
||||||
|
"Run in verbose mode.").
|
||||||
Short('v').
|
Short('v').
|
||||||
Envar("MTG_VERBOSE").
|
Envar("MTG_VERBOSE").
|
||||||
Bool()
|
Bool()
|
||||||
|
|
||||||
bindIP = app.Flag("bind-ip", "Which IP to bind to.").
|
bindIP = app.Flag("bind-ip",
|
||||||
|
"Which IP to bind to.").
|
||||||
Short('b').
|
Short('b').
|
||||||
Envar("MTG_IP").
|
Envar("MTG_IP").
|
||||||
Default("127.0.0.1").
|
Default("127.0.0.1").
|
||||||
IP()
|
IP()
|
||||||
bindPort = app.Flag("bind-port", "Which port to bind to.").
|
bindPort = app.Flag("bind-port",
|
||||||
|
"Which port to bind to.").
|
||||||
Short('p').
|
Short('p').
|
||||||
Envar("MTG_PORT").
|
Envar("MTG_PORT").
|
||||||
Default("3128").
|
Default("3128").
|
||||||
Uint16()
|
Uint16()
|
||||||
|
|
||||||
publicIPv4 = app.Flag("public-ipv4", "Which IPv4 address is public.").
|
publicIPv4 = app.Flag("public-ipv4",
|
||||||
|
"Which IPv4 address is public.").
|
||||||
Short('4').
|
Short('4').
|
||||||
Envar("MTG_IPV4").
|
Envar("MTG_IPV4").
|
||||||
IP()
|
IP()
|
||||||
publicIPv4Port = app.Flag("public-ipv4-port", "Which IPv4 port is public. Default is 'bind-port' value.").
|
publicIPv4Port = app.Flag("public-ipv4-port",
|
||||||
|
"Which IPv4 port is public. Default is 'bind-port' value.").
|
||||||
Envar("MTG_IPV4_PORT").
|
Envar("MTG_IPV4_PORT").
|
||||||
Uint16()
|
Uint16()
|
||||||
|
|
||||||
publicIPv6 = app.Flag("public-ipv6", "Which IPv6 address is public.").
|
publicIPv6 = app.Flag("public-ipv6",
|
||||||
|
"Which IPv6 address is public.").
|
||||||
Short('6').
|
Short('6').
|
||||||
Envar("MTG_IPV6").
|
Envar("MTG_IPV6").
|
||||||
IP()
|
IP()
|
||||||
publicIPv6Port = app.Flag("public-ipv6-port", "Which IPv6 port is public. Default is 'bind-port' value.").
|
publicIPv6Port = app.Flag("public-ipv6-port",
|
||||||
|
"Which IPv6 port is public. Default is 'bind-port' value.").
|
||||||
Envar("MTG_IPV6_PORT").
|
Envar("MTG_IPV6_PORT").
|
||||||
Uint16()
|
Uint16()
|
||||||
|
|
||||||
statsIP = app.Flag("stats-ip", "Which IP bind stats server to").
|
statsIP = app.Flag("stats-ip",
|
||||||
|
"Which IP bind stats server to.").
|
||||||
Short('t').
|
Short('t').
|
||||||
Envar("MTG_STATS_IP").
|
Envar("MTG_STATS_IP").
|
||||||
Default("127.0.0.1").
|
Default("127.0.0.1").
|
||||||
IP()
|
IP()
|
||||||
statsPort = app.Flag("stats-port", "Which port bind stats to.").
|
statsPort = app.Flag("stats-port",
|
||||||
|
"Which port bind stats to.").
|
||||||
Short('q').
|
Short('q').
|
||||||
Envar("MTG_STATS_PORT").
|
Envar("MTG_STATS_PORT").
|
||||||
Default("3129").
|
Default("3129").
|
||||||
Uint16()
|
Uint16()
|
||||||
|
|
||||||
secret = app.Arg("secret", "Secret of this proxy.").Required().String()
|
statsdIP = app.Flag("statsd-ip",
|
||||||
adtag = app.Arg("adtag", "ADTag of the proxy.").String()
|
"Which IP should we use for working with statsd.").
|
||||||
|
Envar("MTG_STATSD_IP").
|
||||||
|
String()
|
||||||
|
statsdPort = app.Flag("statsd-port",
|
||||||
|
"Which port should we use for working with statsd.").
|
||||||
|
Envar("MTG_STATSD_PORT").
|
||||||
|
Default("8125").
|
||||||
|
Uint16()
|
||||||
|
statsdNetwork = app.Flag("statsd-network",
|
||||||
|
"Which network is used to work with statsd. Only 'tcp' and 'udp' are supported.").
|
||||||
|
Envar("MTG_STATSD_NETWORK").
|
||||||
|
Default("udp").
|
||||||
|
String()
|
||||||
|
statsdPrefix = app.Flag("statsd-prefix",
|
||||||
|
"Which bucket prefix should we use for sending stats to statsd.").
|
||||||
|
Envar("MTG_STATSD_PREFIX").
|
||||||
|
Default("mtg").
|
||||||
|
String()
|
||||||
|
statsdTagsFormat = app.Flag("statsd-tags-format",
|
||||||
|
"Which tag format should we use to send stats metrics. Valid options are 'datadog' and 'influxdb'.").
|
||||||
|
Envar("MTG_STATSD_TAGS_FORMAT").
|
||||||
|
String()
|
||||||
|
statsdTags = app.Flag("statsd-tags",
|
||||||
|
"Tags to use for working with statsd (specified as 'key=value').").
|
||||||
|
Envar("MTG_STATSD_TAGS").
|
||||||
|
StringMap()
|
||||||
|
|
||||||
|
writeBufferSize = app.Flag("write-buffer",
|
||||||
|
"Write buffer size in bytes. You can think about it as a buffer from client to Telegram.").
|
||||||
|
Short('w').
|
||||||
|
Envar("MTG_BUFFER_WRITE").
|
||||||
|
Default("65536").
|
||||||
|
Uint32()
|
||||||
|
readBufferSize = app.Flag("read-buffer",
|
||||||
|
"Read buffer size in bytes. You can think about it as a buffer from Telegram to client.").
|
||||||
|
Short('r').
|
||||||
|
Envar("MTG_BUFFER_READ").
|
||||||
|
Default("131072").
|
||||||
|
Uint32()
|
||||||
|
|
||||||
|
secret = app.Arg("secret", "Secret of this proxy.").Required().HexBytes()
|
||||||
|
adtag = app.Arg("adtag", "ADTag of the proxy.").HexBytes()
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rand.Seed(time.Now().UTC().UnixNano())
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
app.Version(version)
|
app.Version(version)
|
||||||
|
app.HelpFlag.Short('h')
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() { // nolint: gocyclo
|
||||||
kingpin.MustParse(app.Parse(os.Args[1:]))
|
kingpin.MustParse(app.Parse(os.Args[1:]))
|
||||||
|
|
||||||
err := setRLimit()
|
err := setRLimit()
|
||||||
@@ -89,10 +142,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
conf, err := config.NewConfig(*debug, *verbose,
|
conf, err := config.NewConfig(*debug, *verbose,
|
||||||
*bindIP, *bindPort,
|
*writeBufferSize, *readBufferSize,
|
||||||
*publicIPv4, *publicIPv4Port,
|
*bindIP, *publicIPv4, *publicIPv6, *statsIP,
|
||||||
*publicIPv6, *publicIPv6Port,
|
*bindPort, *publicIPv4Port, *publicIPv6Port, *statsPort, *statsdPort,
|
||||||
*statsIP, *statsPort,
|
*statsdIP, *statsdNetwork, *statsdPrefix, *statsdTagsFormat,
|
||||||
|
*statsdTags,
|
||||||
*secret, *adtag,
|
*secret, *adtag,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -100,11 +154,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
atom := zap.NewAtomicLevel()
|
atom := zap.NewAtomicLevel()
|
||||||
if conf.Debug {
|
switch {
|
||||||
|
case conf.Debug:
|
||||||
atom.SetLevel(zapcore.DebugLevel)
|
atom.SetLevel(zapcore.DebugLevel)
|
||||||
} else if conf.Verbose {
|
case conf.Verbose:
|
||||||
atom.SetLevel(zapcore.InfoLevel)
|
atom.SetLevel(zapcore.InfoLevel)
|
||||||
} else {
|
default:
|
||||||
atom.SetLevel(zapcore.ErrorLevel)
|
atom.SetLevel(zapcore.ErrorLevel)
|
||||||
}
|
}
|
||||||
encoderCfg := zap.NewProductionEncoderConfig()
|
encoderCfg := zap.NewProductionEncoderConfig()
|
||||||
@@ -120,11 +175,22 @@ func main() {
|
|||||||
|
|
||||||
if conf.UseMiddleProxy() {
|
if conf.UseMiddleProxy() {
|
||||||
zap.S().Infow("Use middle proxy connection to Telegram")
|
zap.S().Infow("Use middle proxy connection to Telegram")
|
||||||
|
if diff, err := ntp.Fetch(); err != nil {
|
||||||
|
zap.S().Warnw("Could not fetch time data from NTP")
|
||||||
|
} else {
|
||||||
|
if diff >= time.Second {
|
||||||
|
usage(fmt.Sprintf("You choose to use middle proxy but your clock drift (%s) "+
|
||||||
|
"is bigger than 1 second. Please, sync your time", diff))
|
||||||
|
}
|
||||||
|
go ntp.AutoUpdate()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
zap.S().Infow("Use direct connection to Telegram")
|
zap.S().Infow("Use direct connection to Telegram")
|
||||||
}
|
}
|
||||||
|
|
||||||
go stats.Start(conf)
|
if err := stats.Start(conf); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
server := proxy.NewProxy(conf)
|
server := proxy.NewProxy(conf)
|
||||||
if err := server.Serve(); err != nil {
|
if err := server.Serve(); err != nil {
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ const (
|
|||||||
ConnectionTypeUnknown ConnectionType = iota
|
ConnectionTypeUnknown ConnectionType = iota
|
||||||
ConnectionTypeAbridged
|
ConnectionTypeAbridged
|
||||||
ConnectionTypeIntermediate
|
ConnectionTypeIntermediate
|
||||||
|
ConnectionTypeSecure
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConnectionProtocol* define which connection protocols to use.
|
// ConnectionProtocol* define which connection protocols to use.
|
||||||
@@ -53,6 +54,7 @@ const (
|
|||||||
var (
|
var (
|
||||||
ConnectionTagAbridged = []byte{0xef, 0xef, 0xef, 0xef}
|
ConnectionTagAbridged = []byte{0xef, 0xef, 0xef, 0xef}
|
||||||
ConnectionTagIntermediate = []byte{0xee, 0xee, 0xee, 0xee}
|
ConnectionTagIntermediate = []byte{0xee, 0xee, 0xee, 0xee}
|
||||||
|
ConnectionTagSecure = []byte{0xdd, 0xdd, 0xdd, 0xdd}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Tag maps connection type to the corresponding handshake tag.
|
// Tag maps connection type to the corresponding handshake tag.
|
||||||
@@ -62,6 +64,8 @@ func (t ConnectionType) Tag() ([]byte, error) {
|
|||||||
return ConnectionTagAbridged, nil
|
return ConnectionTagAbridged, nil
|
||||||
case ConnectionTypeIntermediate:
|
case ConnectionTypeIntermediate:
|
||||||
return ConnectionTagIntermediate, nil
|
return ConnectionTagIntermediate, nil
|
||||||
|
case ConnectionTypeSecure:
|
||||||
|
return ConnectionTagSecure, nil
|
||||||
default:
|
default:
|
||||||
return nil, errors.Errorf("Unknown connection type %d", t)
|
return nil, errors.Errorf("Unknown connection type %d", t)
|
||||||
}
|
}
|
||||||
@@ -75,6 +79,9 @@ func ConnectionTagFromHandshake(magic []byte) (ConnectionType, error) {
|
|||||||
if bytes.Equal(magic, ConnectionTagAbridged) {
|
if bytes.Equal(magic, ConnectionTagAbridged) {
|
||||||
return ConnectionTypeAbridged, nil
|
return ConnectionTypeAbridged, nil
|
||||||
}
|
}
|
||||||
|
if bytes.Equal(magic, ConnectionTagSecure) {
|
||||||
|
return ConnectionTypeSecure, nil
|
||||||
|
}
|
||||||
|
|
||||||
return ConnectionTypeUnknown, errors.New("Unknown handshake protocol")
|
return ConnectionTypeUnknown, errors.New("Unknown handshake protocol")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,10 +19,10 @@ type HandshakeResponse struct {
|
|||||||
func (r *HandshakeResponse) Bytes() []byte {
|
func (r *HandshakeResponse) Bytes() []byte {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
buf.Write(r.Type[:])
|
buf.Write(r.Type)
|
||||||
buf.Write(r.Flags[:])
|
buf.Write(r.Flags)
|
||||||
buf.Write(r.SenderPID[:])
|
buf.Write(r.SenderPID)
|
||||||
buf.Write(r.PeerPID[:])
|
buf.Write(r.PeerPID)
|
||||||
|
|
||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,13 @@ type proxyRequestFlags uint32
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
proxyRequestFlagsHasAdTag proxyRequestFlags = 0x8
|
proxyRequestFlagsHasAdTag proxyRequestFlags = 0x8
|
||||||
proxyRequestFlagsEncrypted = 0x2
|
proxyRequestFlagsEncrypted proxyRequestFlags = 0x2
|
||||||
proxyRequestFlagsMagic = 0x1000
|
proxyRequestFlagsMagic proxyRequestFlags = 0x1000
|
||||||
proxyRequestFlagsExtMode2 = 0x20000
|
proxyRequestFlagsExtMode2 proxyRequestFlags = 0x20000
|
||||||
proxyRequestFlagsIntermediate = 0x20000000
|
proxyRequestFlagsIntermediate proxyRequestFlags = 0x20000000
|
||||||
proxyRequestFlagsAbdridged = 0x40000000
|
proxyRequestFlagsAbdridged proxyRequestFlags = 0x40000000
|
||||||
proxyRequestFlagsQuickAck = 0x80000000
|
proxyRequestFlagsQuickAck proxyRequestFlags = 0x80000000
|
||||||
|
proxyRequestFlagsPad proxyRequestFlags = 0x8000000
|
||||||
)
|
)
|
||||||
|
|
||||||
var proxyRequestFlagsEncryptedPrefix [8]byte
|
var proxyRequestFlagsEncryptedPrefix [8]byte
|
||||||
@@ -50,6 +51,9 @@ func (r proxyRequestFlags) String() string {
|
|||||||
if r&proxyRequestFlagsQuickAck != 0 {
|
if r&proxyRequestFlagsQuickAck != 0 {
|
||||||
flags = append(flags, "QUICK_ACK")
|
flags = append(flags, "QUICK_ACK")
|
||||||
}
|
}
|
||||||
|
if r&proxyRequestFlagsPad != 0 {
|
||||||
|
flags = append(flags, "PAD")
|
||||||
|
}
|
||||||
|
|
||||||
return strings.Join(flags, " | ")
|
return strings.Join(flags, " | ")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,8 @@ func (r *ProxyRequest) MakeHeader(message []byte) (*bytes.Buffer, fmt.Stringer)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewProxyRequest build new ProxyRequest data structure.
|
// NewProxyRequest build new ProxyRequest data structure.
|
||||||
func NewProxyRequest(clientAddr, ownAddr *net.TCPAddr, opts *mtproto.ConnectionOpts, adTag []byte) (*ProxyRequest, error) {
|
func NewProxyRequest(clientAddr, ownAddr *net.TCPAddr,
|
||||||
|
opts *mtproto.ConnectionOpts, adTag []byte) (*ProxyRequest, error) {
|
||||||
flags := proxyRequestFlagsHasAdTag | proxyRequestFlagsMagic | proxyRequestFlagsExtMode2
|
flags := proxyRequestFlagsHasAdTag | proxyRequestFlagsMagic | proxyRequestFlagsExtMode2
|
||||||
|
|
||||||
switch opts.ConnectionType {
|
switch opts.ConnectionType {
|
||||||
@@ -72,6 +73,10 @@ func NewProxyRequest(clientAddr, ownAddr *net.TCPAddr, opts *mtproto.ConnectionO
|
|||||||
flags |= proxyRequestFlagsAbdridged
|
flags |= proxyRequestFlagsAbdridged
|
||||||
case mtproto.ConnectionTypeIntermediate:
|
case mtproto.ConnectionTypeIntermediate:
|
||||||
flags |= proxyRequestFlagsIntermediate
|
flags |= proxyRequestFlagsIntermediate
|
||||||
|
case mtproto.ConnectionTypeSecure:
|
||||||
|
flags |= proxyRequestFlagsIntermediate | proxyRequestFlagsPad
|
||||||
|
default:
|
||||||
|
panic("Unknown connection type")
|
||||||
}
|
}
|
||||||
|
|
||||||
request := &ProxyRequest{
|
request := &ProxyRequest{
|
||||||
|
|||||||
+60
@@ -0,0 +1,60 @@
|
|||||||
|
package ntp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/beevik/ntp"
|
||||||
|
"github.com/juju/errors"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
const autoUpdatePeriod = time.Minute
|
||||||
|
|
||||||
|
var ntpEndpoints = []string{
|
||||||
|
"0.pool.ntp.org",
|
||||||
|
"1.pool.ntp.org",
|
||||||
|
"2.pool.ntp.org",
|
||||||
|
"3.pool.ntp.org",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch fetches the data on time drift.
|
||||||
|
func Fetch() (time.Duration, error) {
|
||||||
|
url := ntpEndpoints[rand.Intn(len(ntpEndpoints))]
|
||||||
|
resp, err := ntp.Query(url)
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.Annotatef(err, "Cannot fetch NTP server %s", url)
|
||||||
|
}
|
||||||
|
|
||||||
|
offsetInt := int64(resp.ClockOffset)
|
||||||
|
if offsetInt < 0 {
|
||||||
|
offsetInt = -offsetInt
|
||||||
|
}
|
||||||
|
offset := time.Duration(offsetInt)
|
||||||
|
|
||||||
|
return offset, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AutoUpdate runs periodic check of current time .drift state.
|
||||||
|
func AutoUpdate() {
|
||||||
|
logger := zap.S().Named("ntp")
|
||||||
|
|
||||||
|
for range time.Tick(autoUpdatePeriod) {
|
||||||
|
diff, err := Fetch()
|
||||||
|
if err != nil {
|
||||||
|
logger.Debugw("Cannot fetch time from NTP", "error", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case diff < 400*time.Millisecond:
|
||||||
|
logger.Debugw("NTP time drift", "value", diff.String())
|
||||||
|
case diff < 600*time.Millisecond:
|
||||||
|
logger.Infow("NTP time drift", "value", diff.String())
|
||||||
|
case diff < 800*time.Millisecond:
|
||||||
|
logger.Warnw("NTP time drift", "value", diff.String())
|
||||||
|
default:
|
||||||
|
logger.Errorw("NTP time drift", "value", diff.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -47,8 +47,8 @@ func TestObfs2Full(t *testing.T) {
|
|||||||
|
|
||||||
clientFrame := generateFrame(mtproto.ConnectionTypeIntermediate)
|
clientFrame := generateFrame(mtproto.ConnectionTypeIntermediate)
|
||||||
clientHasher := sha256.New()
|
clientHasher := sha256.New()
|
||||||
clientHasher.Write(clientFrame.Key())
|
clientHasher.Write(clientFrame.Key()) // nolint: errcheck
|
||||||
clientHasher.Write(secret)
|
clientHasher.Write(secret) // nolint: errcheck
|
||||||
clientKey := clientHasher.Sum(nil)
|
clientKey := clientHasher.Sum(nil)
|
||||||
|
|
||||||
encryptor := makeStreamCipher(clientKey, clientFrame.IV())
|
encryptor := makeStreamCipher(clientKey, clientFrame.IV())
|
||||||
@@ -58,8 +58,8 @@ func TestObfs2Full(t *testing.T) {
|
|||||||
|
|
||||||
invertedClientFrame := clientFrame.Invert()
|
invertedClientFrame := clientFrame.Invert()
|
||||||
clientHasher = sha256.New()
|
clientHasher = sha256.New()
|
||||||
clientHasher.Write(invertedClientFrame.Key())
|
clientHasher.Write(invertedClientFrame.Key()) // nolint: errcheck
|
||||||
clientHasher.Write(secret)
|
clientHasher.Write(secret) // nolint: errcheck
|
||||||
invertedClientKey := clientHasher.Sum(nil)
|
invertedClientKey := clientHasher.Sum(nil)
|
||||||
clientDecryptor := makeStreamCipher(invertedClientKey, invertedClientFrame.IV())
|
clientDecryptor := makeStreamCipher(invertedClientKey, invertedClientFrame.IV())
|
||||||
|
|
||||||
|
|||||||
+15
-9
@@ -1,6 +1,7 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -43,6 +44,7 @@ func (p *Proxy) Serve() error {
|
|||||||
func (p *Proxy) accept(conn net.Conn) {
|
func (p *Proxy) accept(conn net.Conn) {
|
||||||
connID := uuid.NewV4().String()
|
connID := uuid.NewV4().String()
|
||||||
log := zap.S().With("connection_id", connID).Named("main")
|
log := zap.S().With("connection_id", connID).Named("main")
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
conn.Close() // nolint: errcheck
|
conn.Close() // nolint: errcheck
|
||||||
@@ -55,7 +57,7 @@ func (p *Proxy) accept(conn net.Conn) {
|
|||||||
|
|
||||||
log.Infow("Client connected", "addr", conn.RemoteAddr())
|
log.Infow("Client connected", "addr", conn.RemoteAddr())
|
||||||
|
|
||||||
clientConn, opts, err := p.clientInit(conn, connID, p.conf)
|
clientConn, opts, err := p.clientInit(ctx, cancel, conn, connID, p.conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorw("Cannot initialize client connection", "error", err)
|
log.Errorw("Cannot initialize client connection", "error", err)
|
||||||
return
|
return
|
||||||
@@ -65,7 +67,7 @@ func (p *Proxy) accept(conn net.Conn) {
|
|||||||
stats.ClientConnected(opts.ConnectionType, clientConn.RemoteAddr())
|
stats.ClientConnected(opts.ConnectionType, clientConn.RemoteAddr())
|
||||||
defer stats.ClientDisconnected(opts.ConnectionType, clientConn.RemoteAddr())
|
defer stats.ClientDisconnected(opts.ConnectionType, clientConn.RemoteAddr())
|
||||||
|
|
||||||
serverConn, err := p.getTelegramConn(opts, connID)
|
serverConn, err := p.getTelegramConn(ctx, cancel, opts, connID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorw("Cannot initialize server connection", "error", err)
|
log.Errorw("Cannot initialize server connection", "error", err)
|
||||||
return
|
return
|
||||||
@@ -83,8 +85,8 @@ func (p *Proxy) accept(conn net.Conn) {
|
|||||||
} else {
|
} else {
|
||||||
clientStream := clientConn.(wrappers.StreamReadWriteCloser)
|
clientStream := clientConn.(wrappers.StreamReadWriteCloser)
|
||||||
serverStream := serverConn.(wrappers.StreamReadWriteCloser)
|
serverStream := serverConn.(wrappers.StreamReadWriteCloser)
|
||||||
go p.directPipe(clientStream, serverStream, wait)
|
go p.directPipe(clientStream, serverStream, wait, p.conf.ReadBufferSize)
|
||||||
go p.directPipe(serverStream, clientStream, wait)
|
go p.directPipe(serverStream, clientStream, wait, p.conf.WriteBufferSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
wait.Wait()
|
wait.Wait()
|
||||||
@@ -92,8 +94,9 @@ func (p *Proxy) accept(conn net.Conn) {
|
|||||||
log.Infow("Client disconnected", "addr", conn.RemoteAddr())
|
log.Infow("Client disconnected", "addr", conn.RemoteAddr())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Proxy) getTelegramConn(opts *mtproto.ConnectionOpts, connID string) (wrappers.Wrap, error) {
|
func (p *Proxy) getTelegramConn(ctx context.Context, cancel context.CancelFunc,
|
||||||
streamConn, err := p.tg.Dial(connID, opts)
|
opts *mtproto.ConnectionOpts, connID string) (wrappers.Wrap, error) {
|
||||||
|
streamConn, err := p.tg.Dial(ctx, cancel, connID, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot dial to Telegram")
|
return nil, errors.Annotate(err, "Cannot dial to Telegram")
|
||||||
}
|
}
|
||||||
@@ -106,7 +109,8 @@ func (p *Proxy) getTelegramConn(opts *mtproto.ConnectionOpts, connID string) (wr
|
|||||||
return packetConn, nil
|
return packetConn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Proxy) middlePipe(src wrappers.PacketReadCloser, dst io.WriteCloser, wait *sync.WaitGroup, hacks *mtproto.Hacks) {
|
func (p *Proxy) middlePipe(src wrappers.PacketReadCloser, dst io.WriteCloser,
|
||||||
|
wait *sync.WaitGroup, hacks *mtproto.Hacks) {
|
||||||
defer func() {
|
defer func() {
|
||||||
src.Close() // nolint: errcheck
|
src.Close() // nolint: errcheck
|
||||||
dst.Close() // nolint: errcheck
|
dst.Close() // nolint: errcheck
|
||||||
@@ -129,14 +133,16 @@ func (p *Proxy) middlePipe(src wrappers.PacketReadCloser, dst io.WriteCloser, wa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Proxy) directPipe(src wrappers.StreamReadCloser, dst io.WriteCloser, wait *sync.WaitGroup) {
|
func (p *Proxy) directPipe(src wrappers.StreamReadCloser, dst io.WriteCloser,
|
||||||
|
wait *sync.WaitGroup, bufferSize int) {
|
||||||
defer func() {
|
defer func() {
|
||||||
src.Close() // nolint: errcheck
|
src.Close() // nolint: errcheck
|
||||||
dst.Close() // nolint: errcheck
|
dst.Close() // nolint: errcheck
|
||||||
wait.Done()
|
wait.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if _, err := io.Copy(dst, src); err != nil {
|
buffer := make([]byte, bufferSize)
|
||||||
|
if _, err := io.CopyBuffer(dst, src, buffer); err != nil {
|
||||||
src.Logger().Warnw("Cannot pump sockets", "error", err)
|
src.Logger().Warnw("Cannot pump sockets", "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-16
@@ -40,7 +40,7 @@ func crashManager() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func connectionManager() { // nolint: gocyclo
|
func connectionManager() {
|
||||||
for event := range connectionsChan {
|
for event := range connectionsChan {
|
||||||
instance.mutex.RLock()
|
instance.mutex.RLock()
|
||||||
|
|
||||||
@@ -53,27 +53,21 @@ func connectionManager() { // nolint: gocyclo
|
|||||||
switch event.connectionType {
|
switch event.connectionType {
|
||||||
case mtproto.ConnectionTypeAbridged:
|
case mtproto.ConnectionTypeAbridged:
|
||||||
if isIPv4 {
|
if isIPv4 {
|
||||||
instance.ActiveConnections.Abridged.IPv4 += inc
|
instance.Connections.Abridged.IPv4 += inc
|
||||||
if event.connected {
|
|
||||||
instance.AllConnections.Abridged.IPv4 += inc
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
instance.ActiveConnections.Abridged.IPv6 += inc
|
instance.Connections.Abridged.IPv6 += inc
|
||||||
if event.connected {
|
|
||||||
instance.AllConnections.Abridged.IPv6 += inc
|
|
||||||
}
|
}
|
||||||
|
case mtproto.ConnectionTypeSecure:
|
||||||
|
if isIPv4 {
|
||||||
|
instance.Connections.Secure.IPv4 += inc
|
||||||
|
} else {
|
||||||
|
instance.Connections.Secure.IPv6 += inc
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if isIPv4 {
|
if isIPv4 {
|
||||||
instance.ActiveConnections.Intermediate.IPv4 += inc
|
instance.Connections.Intermediate.IPv4 += inc
|
||||||
if event.connected {
|
|
||||||
instance.AllConnections.Intermediate.IPv4 += inc
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
instance.ActiveConnections.Intermediate.IPv6 += inc
|
instance.Connections.Intermediate.IPv6 += inc
|
||||||
if event.connected {
|
|
||||||
instance.AllConnections.Intermediate.IPv6 += inc
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-2
@@ -13,8 +13,8 @@ import (
|
|||||||
|
|
||||||
var instance *stats
|
var instance *stats
|
||||||
|
|
||||||
// Start starts new statisitcs server.
|
// Start starts new statistics server.
|
||||||
func Start(conf *config.Config) {
|
func Start(conf *config.Config) error {
|
||||||
log := zap.S().Named("stats")
|
log := zap.S().Named("stats")
|
||||||
|
|
||||||
instance = &stats{
|
instance = &stats{
|
||||||
@@ -23,6 +23,14 @@ func Start(conf *config.Config) {
|
|||||||
mutex: &sync.RWMutex{},
|
mutex: &sync.RWMutex{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if conf.StatsD.Enabled {
|
||||||
|
client, err := newStatsd(conf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
go client.run()
|
||||||
|
}
|
||||||
|
|
||||||
go crashManager()
|
go crashManager()
|
||||||
go connectionManager()
|
go connectionManager()
|
||||||
go trafficManager()
|
go trafficManager()
|
||||||
@@ -51,7 +59,11 @@ func Start(conf *config.Config) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
go func() {
|
||||||
if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
|
if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
|
||||||
log.Fatalw("Stats server has been stopped", "error", err)
|
log.Fatalw("Stats server has been stopped", "error", err)
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-4
@@ -52,20 +52,23 @@ type connections struct {
|
|||||||
All connectionType `json:"all"`
|
All connectionType `json:"all"`
|
||||||
Abridged connectionType `json:"abridged"`
|
Abridged connectionType `json:"abridged"`
|
||||||
Intermediate connectionType `json:"intermediate"`
|
Intermediate connectionType `json:"intermediate"`
|
||||||
|
Secure connectionType `json:"secure"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c connections) MarshalJSON() ([]byte, error) {
|
func (c connections) MarshalJSON() ([]byte, error) {
|
||||||
c.All.IPv4 = c.Abridged.IPv4 + c.Intermediate.IPv4
|
c.All.IPv4 = c.Abridged.IPv4 + c.Intermediate.IPv4 + c.Secure.IPv4
|
||||||
c.All.IPv6 = c.Abridged.IPv6 + c.Intermediate.IPv6
|
c.All.IPv6 = c.Abridged.IPv6 + c.Intermediate.IPv6 + c.Secure.IPv6
|
||||||
|
|
||||||
value := struct {
|
value := struct {
|
||||||
All connectionType `json:"all"`
|
All connectionType `json:"all"`
|
||||||
Abridged connectionType `json:"abridged"`
|
Abridged connectionType `json:"abridged"`
|
||||||
Intermediate connectionType `json:"intermediate"`
|
Intermediate connectionType `json:"intermediate"`
|
||||||
|
Secure connectionType `json:"secure"`
|
||||||
}{
|
}{
|
||||||
All: c.All,
|
All: c.All,
|
||||||
Abridged: c.Abridged,
|
Abridged: c.Abridged,
|
||||||
Intermediate: c.Intermediate,
|
Intermediate: c.Intermediate,
|
||||||
|
Secure: c.Secure,
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.Marshal(value)
|
return json.Marshal(value)
|
||||||
@@ -88,8 +91,7 @@ type speed struct {
|
|||||||
|
|
||||||
type stats struct {
|
type stats struct {
|
||||||
URLs config.IPURLs `json:"urls"`
|
URLs config.IPURLs `json:"urls"`
|
||||||
ActiveConnections connections `json:"active_connections"`
|
Connections connections `json:"connections"`
|
||||||
AllConnections connections `json:"all_connections"`
|
|
||||||
Traffic traffic `json:"traffic"`
|
Traffic traffic `json:"traffic"`
|
||||||
Speed speed `json:"speed"`
|
Speed speed `json:"speed"`
|
||||||
Uptime uptime `json:"uptime"`
|
Uptime uptime `json:"uptime"`
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package stats
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/juju/errors"
|
||||||
|
statsd "gopkg.in/alexcesaro/statsd.v2"
|
||||||
|
|
||||||
|
"github.com/9seconds/mtg/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
statsdConnectionsAbridgedV4 = "connections.abridged.ipv4"
|
||||||
|
statsdConnectionsAbridgedV6 = "connections.abridged.ipv6"
|
||||||
|
|
||||||
|
statsdConnectionsIntermediateV4 = "connections.intermediate.ipv4"
|
||||||
|
statsdConnectionsIntermediateV6 = "connections.intermediate.ipv6"
|
||||||
|
|
||||||
|
statsdConnectionsSecureV4 = "connections.secure.ipv4"
|
||||||
|
statsdConnectionsSecureV6 = "connections.secure.ipv6"
|
||||||
|
|
||||||
|
statsdTrafficIngress = "traffic.ingress"
|
||||||
|
statsdTrafficEgress = "traffic.egress"
|
||||||
|
|
||||||
|
statsdSpeedIngress = "speed.ingress"
|
||||||
|
statsdSpeedEgress = "speed.egress"
|
||||||
|
|
||||||
|
statsdCrashes = "crashes"
|
||||||
|
)
|
||||||
|
|
||||||
|
const statsdPollTime = time.Second
|
||||||
|
|
||||||
|
type statsdExporter struct {
|
||||||
|
client *statsd.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *statsdExporter) run() {
|
||||||
|
for range time.Tick(statsdPollTime) {
|
||||||
|
instance.mutex.Lock()
|
||||||
|
|
||||||
|
s.client.Gauge(statsdConnectionsAbridgedV4, instance.Connections.Abridged.IPv4)
|
||||||
|
s.client.Gauge(statsdConnectionsAbridgedV6, instance.Connections.Abridged.IPv6)
|
||||||
|
s.client.Gauge(statsdConnectionsIntermediateV4, instance.Connections.Intermediate.IPv4)
|
||||||
|
s.client.Gauge(statsdConnectionsIntermediateV6, instance.Connections.Intermediate.IPv6)
|
||||||
|
s.client.Gauge(statsdConnectionsSecureV4, instance.Connections.Secure.IPv4)
|
||||||
|
s.client.Gauge(statsdConnectionsSecureV6, instance.Connections.Secure.IPv6)
|
||||||
|
s.client.Gauge(statsdTrafficIngress, uint64(instance.Traffic.Ingress))
|
||||||
|
s.client.Gauge(statsdTrafficEgress, uint64(instance.Traffic.Egress))
|
||||||
|
s.client.Gauge(statsdSpeedIngress, uint64(instance.Speed.Ingress))
|
||||||
|
s.client.Gauge(statsdSpeedEgress, uint64(instance.Speed.Egress))
|
||||||
|
s.client.Gauge(statsdCrashes, instance.Crashes)
|
||||||
|
|
||||||
|
instance.mutex.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newStatsd(conf *config.Config) (*statsdExporter, error) {
|
||||||
|
options := []statsd.Option{
|
||||||
|
statsd.Network(conf.StatsD.Addr.Network()),
|
||||||
|
statsd.Address(conf.StatsD.Addr.String()),
|
||||||
|
statsd.Prefix(conf.StatsD.Prefix),
|
||||||
|
}
|
||||||
|
|
||||||
|
if conf.StatsD.TagsFormat > 0 {
|
||||||
|
options = append(options, statsd.TagsFormat(conf.StatsD.TagsFormat))
|
||||||
|
tags := make([]string, len(conf.StatsD.Tags)*2)
|
||||||
|
for k, v := range conf.StatsD.Tags {
|
||||||
|
tags = append(tags, k, v)
|
||||||
|
}
|
||||||
|
options = append(options, statsd.Tags(tags...))
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := statsd.New(options...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Annotate(err, "Cannot create statsd client")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &statsdExporter{client: client}, nil
|
||||||
|
}
|
||||||
+8
-9
@@ -1,6 +1,7 @@
|
|||||||
package telegram
|
package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -10,11 +11,7 @@ import (
|
|||||||
"github.com/9seconds/mtg/wrappers"
|
"github.com/9seconds/mtg/wrappers"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const telegramDialTimeout = 10 * time.Second
|
||||||
telegramDialTimeout = 10 * time.Second
|
|
||||||
readBufferSize = 64 * 1024
|
|
||||||
writeBufferSize = 64 * 1024
|
|
||||||
)
|
|
||||||
|
|
||||||
type tgDialer struct {
|
type tgDialer struct {
|
||||||
net.Dialer
|
net.Dialer
|
||||||
@@ -32,22 +29,24 @@ func (t *tgDialer) dial(addr string) (net.Conn, error) {
|
|||||||
if err = tcpSocket.SetNoDelay(true); err != nil {
|
if err = tcpSocket.SetNoDelay(true); err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot set NO_DELAY to Telegram")
|
return nil, errors.Annotate(err, "Cannot set NO_DELAY to Telegram")
|
||||||
}
|
}
|
||||||
if err = tcpSocket.SetReadBuffer(readBufferSize); err != nil {
|
if err = tcpSocket.SetReadBuffer(t.conf.WriteBufferSize); err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot set read buffer size on telegram socket")
|
return nil, errors.Annotate(err, "Cannot set read buffer size on telegram socket")
|
||||||
}
|
}
|
||||||
if err = tcpSocket.SetWriteBuffer(writeBufferSize); err != nil {
|
if err = tcpSocket.SetWriteBuffer(t.conf.ReadBufferSize); err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot set write buffer size on telegram socket")
|
return nil, errors.Annotate(err, "Cannot set write buffer size on telegram socket")
|
||||||
}
|
}
|
||||||
|
|
||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tgDialer) dialRWC(addr, connID string) (wrappers.StreamReadWriteCloser, error) {
|
func (t *tgDialer) dialRWC(ctx context.Context, cancel context.CancelFunc,
|
||||||
|
addr, connID string) (wrappers.StreamReadWriteCloser, error) {
|
||||||
conn, err := t.dial(addr)
|
conn, err := t.dial(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tgConn := wrappers.NewConn(conn, connID, wrappers.ConnPurposeTelegram, t.conf.PublicIPv4, t.conf.PublicIPv6)
|
tgConn := wrappers.NewConn(ctx, cancel, conn, connID,
|
||||||
|
wrappers.ConnPurposeTelegram, t.conf.PublicIPv4, t.conf.PublicIPv6)
|
||||||
|
|
||||||
return tgConn, nil
|
return tgConn, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-15
@@ -1,6 +1,7 @@
|
|||||||
package telegram
|
package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
@@ -13,18 +14,18 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
directV4Addresses = map[int16][]string{
|
directV4Addresses = map[int16][]string{
|
||||||
0: []string{"149.154.175.50:443"},
|
0: {"149.154.175.50:443"},
|
||||||
1: []string{"149.154.167.51:443"},
|
1: {"149.154.167.51:443"},
|
||||||
2: []string{"149.154.175.100:443"},
|
2: {"149.154.175.100:443"},
|
||||||
3: []string{"149.154.167.91:443"},
|
3: {"149.154.167.91:443"},
|
||||||
4: []string{"149.154.171.5:443"},
|
4: {"149.154.171.5:443"},
|
||||||
}
|
}
|
||||||
directV6Addresses = map[int16][]string{
|
directV6Addresses = map[int16][]string{
|
||||||
0: []string{"[2001:b28:f23d:f001::a]:443"},
|
0: {"[2001:b28:f23d:f001::a]:443"},
|
||||||
1: []string{"[2001:67c:04e8:f002::a]:443"},
|
1: {"[2001:67c:04e8:f002::a]:443"},
|
||||||
2: []string{"[2001:b28:f23d:f003::a]:443"},
|
2: {"[2001:b28:f23d:f003::a]:443"},
|
||||||
3: []string{"[2001:67c:04e8:f004::a]:443"},
|
3: {"[2001:67c:04e8:f004::a]:443"},
|
||||||
4: []string{"[2001:b28:f23f:f005::a]:443"},
|
4: {"[2001:b28:f23f:f005::a]:443"},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,7 +33,8 @@ type directTelegram struct {
|
|||||||
baseTelegram
|
baseTelegram
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *directTelegram) Dial(connID string, connOpts *mtproto.ConnectionOpts) (wrappers.StreamReadWriteCloser, error) {
|
func (t *directTelegram) Dial(ctx context.Context, cancel context.CancelFunc,
|
||||||
|
connID string, connOpts *mtproto.ConnectionOpts) (wrappers.StreamReadWriteCloser, error) {
|
||||||
dc := connOpts.DC
|
dc := connOpts.DC
|
||||||
if dc < 0 {
|
if dc < 0 {
|
||||||
dc = -dc
|
dc = -dc
|
||||||
@@ -40,10 +42,11 @@ func (t *directTelegram) Dial(connID string, connOpts *mtproto.ConnectionOpts) (
|
|||||||
dc = 1
|
dc = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return t.baseTelegram.dial(dc-1, connID, connOpts.ConnectionProto)
|
return t.baseTelegram.dial(ctx, cancel, dc-1, connID, connOpts.ConnectionProto)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *directTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.StreamReadWriteCloser) (wrappers.Wrap, error) {
|
func (t *directTelegram) Init(connOpts *mtproto.ConnectionOpts,
|
||||||
|
conn wrappers.StreamReadWriteCloser) (wrappers.Wrap, error) {
|
||||||
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame(connOpts)
|
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame(connOpts)
|
||||||
|
|
||||||
if _, err := conn.Write(frame); err != nil {
|
if _, err := conn.Write(frame); err != nil {
|
||||||
@@ -56,12 +59,14 @@ func (t *directTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.St
|
|||||||
// NewDirectTelegram returns Telegram instance which connects directly
|
// NewDirectTelegram returns Telegram instance which connects directly
|
||||||
// to Telegram bypassing middleproxies.
|
// to Telegram bypassing middleproxies.
|
||||||
func NewDirectTelegram(conf *config.Config) Telegram {
|
func NewDirectTelegram(conf *config.Config) Telegram {
|
||||||
return &directTelegram{baseTelegram{
|
return &directTelegram{
|
||||||
|
baseTelegram: baseTelegram{
|
||||||
dialer: tgDialer{
|
dialer: tgDialer{
|
||||||
Dialer: net.Dialer{Timeout: telegramDialTimeout},
|
Dialer: net.Dialer{Timeout: telegramDialTimeout},
|
||||||
conf: conf,
|
conf: conf,
|
||||||
},
|
},
|
||||||
v4Addresses: directV4Addresses,
|
v4Addresses: directV4Addresses,
|
||||||
v6Addresses: directV6Addresses,
|
v6Addresses: directV6Addresses,
|
||||||
}}
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-3
@@ -20,7 +20,8 @@ type middleTelegram struct {
|
|||||||
conf *config.Config
|
conf *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *middleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.StreamReadWriteCloser) (wrappers.Wrap, error) {
|
func (t *middleTelegram) Init(connOpts *mtproto.ConnectionOpts,
|
||||||
|
conn wrappers.StreamReadWriteCloser) (wrappers.Wrap, error) {
|
||||||
rpcNonceConn := wrappers.NewMTProtoFrame(conn, rpc.SeqNoNonce)
|
rpcNonceConn := wrappers.NewMTProtoFrame(conn, rpc.SeqNoNonce)
|
||||||
|
|
||||||
rpcNonceReq, err := t.sendRPCNonceRequest(rpcNonceConn)
|
rpcNonceReq, err := t.sendRPCNonceRequest(rpcNonceConn)
|
||||||
@@ -65,7 +66,8 @@ func (t *middleTelegram) sendRPCNonceRequest(conn io.Writer) (*rpc.NonceRequest,
|
|||||||
return rpcNonceReq, nil
|
return rpcNonceReq, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *middleTelegram) receiveRPCNonceResponse(conn wrappers.PacketReader, req *rpc.NonceRequest) (*rpc.NonceResponse, error) {
|
func (t *middleTelegram) receiveRPCNonceResponse(conn wrappers.PacketReader,
|
||||||
|
req *rpc.NonceRequest) (*rpc.NonceResponse, error) {
|
||||||
packet, err := conn.Read()
|
packet, err := conn.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot read RPC nonce response")
|
return nil, errors.Annotate(err, "Cannot read RPC nonce response")
|
||||||
@@ -91,7 +93,8 @@ func (t *middleTelegram) sendRPCHandshakeRequest(conn io.Writer) (*rpc.Handshake
|
|||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *middleTelegram) receiveRPCHandshakeResponse(conn wrappers.PacketReader, req *rpc.HandshakeRequest) (*rpc.HandshakeResponse, error) {
|
func (t *middleTelegram) receiveRPCHandshakeResponse(conn wrappers.PacketReader,
|
||||||
|
req *rpc.HandshakeRequest) (*rpc.HandshakeResponse, error) {
|
||||||
packet, err := conn.Read()
|
packet, err := conn.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot read RPC handshake response")
|
return nil, errors.Annotate(err, "Cannot read RPC handshake response")
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package telegram
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -38,7 +39,8 @@ type middleTelegramCaller struct {
|
|||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *middleTelegramCaller) Dial(connID string, connOpts *mtproto.ConnectionOpts) (wrappers.StreamReadWriteCloser, error) {
|
func (t *middleTelegramCaller) Dial(ctx context.Context, cancel context.CancelFunc, connID string,
|
||||||
|
connOpts *mtproto.ConnectionOpts) (wrappers.StreamReadWriteCloser, error) {
|
||||||
dc := connOpts.DC
|
dc := connOpts.DC
|
||||||
if dc == 0 {
|
if dc == 0 {
|
||||||
dc = 1
|
dc = 1
|
||||||
@@ -46,7 +48,7 @@ func (t *middleTelegramCaller) Dial(connID string, connOpts *mtproto.ConnectionO
|
|||||||
t.dialerMutex.RLock()
|
t.dialerMutex.RLock()
|
||||||
defer t.dialerMutex.RUnlock()
|
defer t.dialerMutex.RUnlock()
|
||||||
|
|
||||||
return t.baseTelegram.dial(dc, connID, connOpts.ConnectionProto)
|
return t.baseTelegram.dial(ctx, cancel, dc, connID, connOpts.ConnectionProto)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *middleTelegramCaller) autoUpdate() {
|
func (t *middleTelegramCaller) autoUpdate() {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package telegram
|
package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
@@ -11,7 +12,7 @@ import (
|
|||||||
|
|
||||||
// Telegram is an interface for different Telegram work modes.
|
// Telegram is an interface for different Telegram work modes.
|
||||||
type Telegram interface {
|
type Telegram interface {
|
||||||
Dial(string, *mtproto.ConnectionOpts) (wrappers.StreamReadWriteCloser, error)
|
Dial(context.Context, context.CancelFunc, string, *mtproto.ConnectionOpts) (wrappers.StreamReadWriteCloser, error)
|
||||||
Init(*mtproto.ConnectionOpts, wrappers.StreamReadWriteCloser) (wrappers.Wrap, error)
|
Init(*mtproto.ConnectionOpts, wrappers.StreamReadWriteCloser) (wrappers.Wrap, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,7 +23,8 @@ type baseTelegram struct {
|
|||||||
v6Addresses map[int16][]string
|
v6Addresses map[int16][]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *baseTelegram) dial(dcIdx int16, connID string, proto mtproto.ConnectionProtocol) (wrappers.StreamReadWriteCloser, error) {
|
func (b *baseTelegram) dial(ctx context.Context, cancel context.CancelFunc, dcIdx int16, connID string,
|
||||||
|
proto mtproto.ConnectionProtocol) (wrappers.StreamReadWriteCloser, error) {
|
||||||
addrs := make([]string, 2)
|
addrs := make([]string, 2)
|
||||||
|
|
||||||
if proto&mtproto.ConnectionProtocolIPv6 != 0 {
|
if proto&mtproto.ConnectionProtocolIPv6 != 0 {
|
||||||
@@ -37,7 +39,7 @@ func (b *baseTelegram) dial(dcIdx int16, connID string, proto mtproto.Connection
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
if conn, err := b.dialer.dialRWC(addr, connID); err == nil {
|
if conn, err := b.dialer.dialRWC(ctx, cancel, addr, connID); err == nil {
|
||||||
return conn, err
|
return conn, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-5
@@ -1,15 +1,17 @@
|
|||||||
package wrappers
|
package wrappers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/stats"
|
"github.com/9seconds/mtg/stats"
|
||||||
|
"github.com/juju/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConnPurpose is intented to be identifier of connection purpose. We
|
// ConnPurpose is intended to be identifier of connection purpose. We
|
||||||
// sometimes want to treat client/telegram connection differently (for
|
// sometimes want to treat client/telegram connection differently (for
|
||||||
// logging for example).
|
// logging for example).
|
||||||
type ConnPurpose uint8
|
type ConnPurpose uint8
|
||||||
@@ -39,8 +41,10 @@ const (
|
|||||||
// Conn is a basic wrapper for net.Conn providing the most low-level
|
// Conn is a basic wrapper for net.Conn providing the most low-level
|
||||||
// logic and management as possible.
|
// logic and management as possible.
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
connID string
|
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
connID string
|
||||||
logger *zap.SugaredLogger
|
logger *zap.SugaredLogger
|
||||||
|
|
||||||
publicIPv4 net.IP
|
publicIPv4 net.IP
|
||||||
@@ -48,28 +52,46 @@ type Conn struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) Write(p []byte) (int, error) {
|
func (c *Conn) Write(p []byte) (int, error) {
|
||||||
|
select {
|
||||||
|
case <-c.ctx.Done():
|
||||||
|
return 0, errors.Annotate(c.ctx.Err(), "Cannot write because context was closed")
|
||||||
|
default:
|
||||||
c.conn.SetWriteDeadline(time.Now().Add(connTimeoutWrite)) // nolint: errcheck
|
c.conn.SetWriteDeadline(time.Now().Add(connTimeoutWrite)) // nolint: errcheck
|
||||||
n, err := c.conn.Write(p)
|
n, err := c.conn.Write(p)
|
||||||
|
if err != nil {
|
||||||
|
c.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
c.logger.Debugw("Write to stream", "bytes", n, "error", err)
|
c.logger.Debugw("Write to stream", "bytes", n, "error", err)
|
||||||
stats.EgressTraffic(n)
|
stats.EgressTraffic(n)
|
||||||
|
|
||||||
return n, err
|
return n, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) Read(p []byte) (int, error) {
|
func (c *Conn) Read(p []byte) (int, error) {
|
||||||
|
select {
|
||||||
|
case <-c.ctx.Done():
|
||||||
|
return 0, errors.Annotate(c.ctx.Err(), "Cannot read because context was closed")
|
||||||
|
default:
|
||||||
c.conn.SetReadDeadline(time.Now().Add(connTimeoutRead)) // nolint: errcheck
|
c.conn.SetReadDeadline(time.Now().Add(connTimeoutRead)) // nolint: errcheck
|
||||||
n, err := c.conn.Read(p)
|
n, err := c.conn.Read(p)
|
||||||
|
if err != nil {
|
||||||
|
c.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
c.logger.Debugw("Read from stream", "bytes", n, "error", err)
|
c.logger.Debugw("Read from stream", "bytes", n, "error", err)
|
||||||
stats.IngressTraffic(n)
|
stats.IngressTraffic(n)
|
||||||
|
|
||||||
return n, err
|
return n, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes underlying net.Conn instance.
|
// Close closes underlying net.Conn instance.
|
||||||
func (c *Conn) Close() error {
|
func (c *Conn) Close() error {
|
||||||
defer c.logger.Debugw("Close connection")
|
defer c.logger.Debugw("Close connection")
|
||||||
|
|
||||||
|
c.cancel()
|
||||||
return c.conn.Close()
|
return c.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +122,8 @@ func (c *Conn) RemoteAddr() *net.TCPAddr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewConn initializes Conn wrapper for net.Conn.
|
// NewConn initializes Conn wrapper for net.Conn.
|
||||||
func NewConn(conn net.Conn, connID string, purpose ConnPurpose, publicIPv4, publicIPv6 net.IP) StreamReadWriteCloser {
|
func NewConn(ctx context.Context, cancel context.CancelFunc, conn net.Conn,
|
||||||
|
connID string, purpose ConnPurpose, publicIPv4, publicIPv6 net.IP) StreamReadWriteCloser {
|
||||||
logger := zap.S().With(
|
logger := zap.S().With(
|
||||||
"connection_id", connID,
|
"connection_id", connID,
|
||||||
"local_address", conn.LocalAddr(),
|
"local_address", conn.LocalAddr(),
|
||||||
@@ -109,9 +132,11 @@ func NewConn(conn net.Conn, connID string, purpose ConnPurpose, publicIPv4, publ
|
|||||||
).Named("conn")
|
).Named("conn")
|
||||||
|
|
||||||
wrapper := Conn{
|
wrapper := Conn{
|
||||||
logger: logger,
|
|
||||||
connID: connID,
|
|
||||||
conn: conn,
|
conn: conn,
|
||||||
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
|
connID: connID,
|
||||||
|
logger: logger,
|
||||||
publicIPv4: publicIPv4,
|
publicIPv4: publicIPv4,
|
||||||
publicIPv6: publicIPv6,
|
publicIPv6: publicIPv6,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ var emptyIP = [4]byte{0x00, 0x00, 0x00, 0x00}
|
|||||||
|
|
||||||
// NewMiddleProxyCipher creates new block cipher to proxy<->telegram
|
// NewMiddleProxyCipher creates new block cipher to proxy<->telegram
|
||||||
// connection.
|
// connection.
|
||||||
func NewMiddleProxyCipher(conn StreamReadWriteCloser, req *rpc.NonceRequest, resp *rpc.NonceResponse, secret []byte) StreamReadWriteCloser {
|
func NewMiddleProxyCipher(conn StreamReadWriteCloser,
|
||||||
|
req *rpc.NonceRequest, resp *rpc.NonceResponse, secret []byte) StreamReadWriteCloser {
|
||||||
localAddr := conn.LocalAddr()
|
localAddr := conn.LocalAddr()
|
||||||
remoteAddr := conn.RemoteAddr()
|
remoteAddr := conn.RemoteAddr()
|
||||||
|
|
||||||
@@ -37,11 +38,12 @@ func NewMiddleProxyCipher(conn StreamReadWriteCloser, req *rpc.NonceRequest, res
|
|||||||
return NewBlockCipher(conn, enc, dec)
|
return NewBlockCipher(conn, enc, dec)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deriveKeys(purpose cipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceResponse, client *net.TCPAddr, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
|
func deriveKeys(purpose cipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceResponse,
|
||||||
|
client, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
|
||||||
message := bytes.Buffer{}
|
message := bytes.Buffer{}
|
||||||
message.Write(resp.Nonce[:])
|
message.Write(resp.Nonce)
|
||||||
message.Write(req.Nonce[:])
|
message.Write(req.Nonce)
|
||||||
message.Write(req.CryptoTS[:])
|
message.Write(req.CryptoTS)
|
||||||
|
|
||||||
clientIPv4 := emptyIP[:]
|
clientIPv4 := emptyIP[:]
|
||||||
serverIPv4 := emptyIP[:]
|
serverIPv4 := emptyIP[:]
|
||||||
@@ -68,13 +70,13 @@ func deriveKeys(purpose cipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceRes
|
|||||||
binary.LittleEndian.PutUint16(port[:], uint16(remote.Port))
|
binary.LittleEndian.PutUint16(port[:], uint16(remote.Port))
|
||||||
message.Write(port[:])
|
message.Write(port[:])
|
||||||
message.Write(secret)
|
message.Write(secret)
|
||||||
message.Write(resp.Nonce[:])
|
message.Write(resp.Nonce)
|
||||||
|
|
||||||
if client.IP.To4() == nil {
|
if client.IP.To4() == nil {
|
||||||
message.Write(client.IP.To16())
|
message.Write(client.IP.To16())
|
||||||
message.Write(remote.IP.To16())
|
message.Write(remote.IP.To16())
|
||||||
}
|
}
|
||||||
message.Write(req.Nonce[:])
|
message.Write(req.Nonce)
|
||||||
|
|
||||||
data := message.Bytes()
|
data := message.Bytes()
|
||||||
md5sum := md5.Sum(data[1:]) // nolint: gas
|
md5sum := md5.Sum(data[1:]) // nolint: gas
|
||||||
|
|||||||
@@ -61,7 +61,8 @@ func (m *MTProtoFrame) Read() ([]byte, error) { // nolint: gocyclo
|
|||||||
"messageLength", messageLength,
|
"messageLength", messageLength,
|
||||||
"sequence_number", m.readSeqNo,
|
"sequence_number", m.readSeqNo,
|
||||||
)
|
)
|
||||||
if messageLength%4 != 0 || messageLength < mtprotoFrameMinMessageLength || messageLength > mtprotoFrameMaxMessageLength {
|
if messageLength%4 != 0 || messageLength < mtprotoFrameMinMessageLength ||
|
||||||
|
messageLength > mtprotoFrameMaxMessageLength {
|
||||||
return nil, errors.Errorf("Incorrect frame message length %d", messageLength)
|
return nil, errors.Errorf("Incorrect frame message length %d", messageLength)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,10 +62,6 @@ func (m *MTProtoIntermediate) Read() ([]byte, error) {
|
|||||||
return nil, errors.Annotate(err, "Cannot read the message")
|
return nil, errors.Annotate(err, "Cannot read the message")
|
||||||
}
|
}
|
||||||
|
|
||||||
if length%4 != 0 {
|
|
||||||
length -= length % 4
|
|
||||||
}
|
|
||||||
|
|
||||||
return buf.Bytes()[:length], nil
|
return buf.Bytes()[:length], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +76,7 @@ func (m *MTProtoIntermediate) Write(p []byte) (int, error) {
|
|||||||
"counter", m.writeCounter,
|
"counter", m.writeCounter,
|
||||||
)
|
)
|
||||||
|
|
||||||
if m.opts.ReadHacks.SimpleAck {
|
if m.opts.WriteHacks.SimpleAck {
|
||||||
return m.conn.Write(p)
|
return m.conn.Write(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package wrappers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
|
"github.com/9seconds/mtg/mtproto"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MTProtoIntermediateSecure is an extension of MTProtoIntermediate
|
||||||
|
// mode which supports random paddings (socalled 'secure mode' or
|
||||||
|
// 'dd-secrets').
|
||||||
|
type MTProtoIntermediateSecure struct {
|
||||||
|
MTProtoIntermediate
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MTProtoIntermediateSecure) Read() ([]byte, error) {
|
||||||
|
data, err := m.MTProtoIntermediate.Read()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
length := len(data) - (len(data) % 4)
|
||||||
|
|
||||||
|
return data[:length], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MTProtoIntermediateSecure) Write(p []byte) (int, error) {
|
||||||
|
defer func() {
|
||||||
|
m.writeCounter++
|
||||||
|
}()
|
||||||
|
|
||||||
|
m.logger.Debugw("Write packet",
|
||||||
|
"simple_ack", m.opts.WriteHacks.SimpleAck,
|
||||||
|
"quick_ack", m.opts.WriteHacks.QuickAck,
|
||||||
|
"counter", m.writeCounter,
|
||||||
|
)
|
||||||
|
|
||||||
|
if m.opts.WriteHacks.SimpleAck {
|
||||||
|
return m.conn.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
paddingLength := rand.Intn(4)
|
||||||
|
buf.Grow(4 + len(p) + paddingLength)
|
||||||
|
|
||||||
|
binary.Write(buf, binary.LittleEndian, uint32(len(p)+paddingLength)) // nolint: errcheck
|
||||||
|
buf.Write(p)
|
||||||
|
buf.Write(make([]byte, paddingLength))
|
||||||
|
|
||||||
|
m.logger.Debugw("Write packet with padding",
|
||||||
|
"simple_ack", m.opts.WriteHacks.SimpleAck,
|
||||||
|
"quick_ack", m.opts.WriteHacks.QuickAck,
|
||||||
|
"counter", m.writeCounter,
|
||||||
|
"padding_length", paddingLength,
|
||||||
|
"length", len(p),
|
||||||
|
)
|
||||||
|
|
||||||
|
_, err := m.conn.Write(buf.Bytes())
|
||||||
|
|
||||||
|
return len(p), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMTProtoIntermediateSecure create new instance of
|
||||||
|
// MTProtoIntermediateSecure instance.
|
||||||
|
func NewMTProtoIntermediateSecure(conn StreamReadWriteCloser, opts *mtproto.ConnectionOpts) PacketReadWriteCloser {
|
||||||
|
return &MTProtoIntermediateSecure{
|
||||||
|
MTProtoIntermediate: MTProtoIntermediate{
|
||||||
|
conn: conn,
|
||||||
|
logger: conn.Logger().Named("mtproto-intermediate-secure"),
|
||||||
|
opts: opts,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -56,7 +56,7 @@ func (m *MTProtoProxy) Read() ([]byte, error) {
|
|||||||
case bytes.Equal(tag, rpc.TagSimpleAck):
|
case bytes.Equal(tag, rpc.TagSimpleAck):
|
||||||
return m.readSimpleAck(packet)
|
return m.readSimpleAck(packet)
|
||||||
case bytes.Equal(tag, rpc.TagCloseExt):
|
case bytes.Equal(tag, rpc.TagCloseExt):
|
||||||
return m.readCloseExt(packet)
|
return m.readCloseExt()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, errors.Errorf("Unknown RPC answer %v", tag)
|
return nil, errors.Errorf("Unknown RPC answer %v", tag)
|
||||||
@@ -91,7 +91,7 @@ func (m *MTProtoProxy) readSimpleAck(data []byte) ([]byte, error) {
|
|||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MTProtoProxy) readCloseExt(data []byte) ([]byte, error) {
|
func (m *MTProtoProxy) readCloseExt() ([]byte, error) {
|
||||||
m.logger.Debugw("Read RPC_CLOSE_EXT", "counter", m.readCounter)
|
m.logger.Debugw("Read RPC_CLOSE_EXT", "counter", m.readCounter)
|
||||||
|
|
||||||
return nil, errors.New("Connection has been closed remotely by RPC call")
|
return nil, errors.New("Connection has been closed remotely by RPC call")
|
||||||
@@ -150,7 +150,8 @@ func (m *MTProtoProxy) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewMTProtoProxy creates new RPC wrapper.
|
// NewMTProtoProxy creates new RPC wrapper.
|
||||||
func NewMTProtoProxy(conn PacketReadWriteCloser, connOpts *mtproto.ConnectionOpts, adTag []byte) (PacketReadWriteCloser, error) {
|
func NewMTProtoProxy(conn PacketReadWriteCloser, connOpts *mtproto.ConnectionOpts,
|
||||||
|
adTag []byte) (PacketReadWriteCloser, error) {
|
||||||
req, err := rpc.NewProxyRequest(connOpts.ClientAddr, conn.LocalAddr(), connOpts, adTag)
|
req, err := rpc.NewProxyRequest(connOpts.ClientAddr, conn.LocalAddr(), connOpts, adTag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot create new RPC proxy request")
|
return nil, errors.Annotate(err, "Cannot create new RPC proxy request")
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package wrappers
|
package wrappers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
@@ -28,10 +29,17 @@ func (s *StreamCipher) Read(p []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamCipher) Write(p []byte) (int, error) {
|
func (s *StreamCipher) Write(p []byte) (int, error) {
|
||||||
encrypted := make([]byte, len(p))
|
buf := streamCipherBufferPool.Get().(*bytes.Buffer)
|
||||||
s.encryptor.XORKeyStream(encrypted, p)
|
defer streamCipherBufferPool.Put(buf)
|
||||||
|
|
||||||
return s.conn.Write(encrypted)
|
buf.Reset()
|
||||||
|
buf.Grow(len(p))
|
||||||
|
buf.Write(p)
|
||||||
|
|
||||||
|
data := buf.Bytes()
|
||||||
|
s.encryptor.XORKeyStream(data, data)
|
||||||
|
|
||||||
|
return s.conn.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logger returns an instance of the logger for this wrapper.
|
// Logger returns an instance of the logger for this wrapper.
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package wrappers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var streamCipherBufferPool sync.Pool
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
streamCipherBufferPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return &bytes.Buffer{}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user