├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md └── src ├── error.rs ├── main.rs └── server.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | .idea/ 3 | *.iml -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "mio-proxy-rs" 3 | version = "0.1.0" 4 | dependencies = [ 5 | "clap 2.24.1 (registry+https://github.com/rust-lang/crates.io-index)", 6 | "env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", 7 | "futures 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", 8 | "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", 9 | "tokio-core 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", 10 | ] 11 | 12 | [[package]] 13 | name = "aho-corasick" 14 | version = "0.5.3" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | dependencies = [ 17 | "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", 18 | ] 19 | 20 | [[package]] 21 | name = "ansi_term" 22 | version = "0.9.0" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | 25 | [[package]] 26 | name = "atty" 27 | version = "0.2.2" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | dependencies = [ 30 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 31 | "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", 32 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 33 | ] 34 | 35 | [[package]] 36 | name = "bitflags" 37 | version = "0.8.2" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | 40 | [[package]] 41 | name = "byteorder" 42 | version = "1.0.0" 43 | source = "registry+https://github.com/rust-lang/crates.io-index" 44 | 45 | [[package]] 46 | name = "bytes" 47 | version = "0.4.3" 48 | source = "registry+https://github.com/rust-lang/crates.io-index" 49 | dependencies = [ 50 | "byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 51 | "iovec 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 52 | ] 53 | 54 | [[package]] 55 | name = "cfg-if" 56 | version = "0.1.0" 57 | source = "registry+https://github.com/rust-lang/crates.io-index" 58 | 59 | [[package]] 60 | name = "clap" 61 | version = "2.24.1" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | dependencies = [ 64 | "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", 65 | "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 66 | "bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", 67 | "strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", 68 | "term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 69 | "unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 70 | "unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 71 | "vec_map 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", 72 | ] 73 | 74 | [[package]] 75 | name = "env_logger" 76 | version = "0.3.5" 77 | source = "registry+https://github.com/rust-lang/crates.io-index" 78 | dependencies = [ 79 | "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", 80 | "regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)", 81 | ] 82 | 83 | [[package]] 84 | name = "futures" 85 | version = "0.1.13" 86 | source = "registry+https://github.com/rust-lang/crates.io-index" 87 | 88 | [[package]] 89 | name = "iovec" 90 | version = "0.1.0" 91 | source = "registry+https://github.com/rust-lang/crates.io-index" 92 | dependencies = [ 93 | "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", 94 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 95 | ] 96 | 97 | [[package]] 98 | name = "kernel32-sys" 99 | version = "0.2.2" 100 | source = "registry+https://github.com/rust-lang/crates.io-index" 101 | dependencies = [ 102 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 103 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 104 | ] 105 | 106 | [[package]] 107 | name = "lazycell" 108 | version = "0.4.0" 109 | source = "registry+https://github.com/rust-lang/crates.io-index" 110 | 111 | [[package]] 112 | name = "libc" 113 | version = "0.2.22" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | 116 | [[package]] 117 | name = "log" 118 | version = "0.3.7" 119 | source = "registry+https://github.com/rust-lang/crates.io-index" 120 | 121 | [[package]] 122 | name = "memchr" 123 | version = "0.1.11" 124 | source = "registry+https://github.com/rust-lang/crates.io-index" 125 | dependencies = [ 126 | "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", 127 | ] 128 | 129 | [[package]] 130 | name = "mio" 131 | version = "0.6.7" 132 | source = "registry+https://github.com/rust-lang/crates.io-index" 133 | dependencies = [ 134 | "iovec 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 135 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 136 | "lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 137 | "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", 138 | "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", 139 | "miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 140 | "net2 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)", 141 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 142 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 143 | ] 144 | 145 | [[package]] 146 | name = "miow" 147 | version = "0.2.1" 148 | source = "registry+https://github.com/rust-lang/crates.io-index" 149 | dependencies = [ 150 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 151 | "net2 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)", 152 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 153 | "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 154 | ] 155 | 156 | [[package]] 157 | name = "net2" 158 | version = "0.2.29" 159 | source = "registry+https://github.com/rust-lang/crates.io-index" 160 | dependencies = [ 161 | "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 162 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 163 | "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", 164 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 165 | "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 166 | ] 167 | 168 | [[package]] 169 | name = "regex" 170 | version = "0.1.80" 171 | source = "registry+https://github.com/rust-lang/crates.io-index" 172 | dependencies = [ 173 | "aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", 174 | "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", 175 | "regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", 176 | "thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", 177 | "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", 178 | ] 179 | 180 | [[package]] 181 | name = "regex-syntax" 182 | version = "0.3.9" 183 | source = "registry+https://github.com/rust-lang/crates.io-index" 184 | 185 | [[package]] 186 | name = "scoped-tls" 187 | version = "0.1.0" 188 | source = "registry+https://github.com/rust-lang/crates.io-index" 189 | 190 | [[package]] 191 | name = "slab" 192 | version = "0.3.0" 193 | source = "registry+https://github.com/rust-lang/crates.io-index" 194 | 195 | [[package]] 196 | name = "strsim" 197 | version = "0.6.0" 198 | source = "registry+https://github.com/rust-lang/crates.io-index" 199 | 200 | [[package]] 201 | name = "term_size" 202 | version = "0.3.0" 203 | source = "registry+https://github.com/rust-lang/crates.io-index" 204 | dependencies = [ 205 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 206 | "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", 207 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 208 | ] 209 | 210 | [[package]] 211 | name = "thread-id" 212 | version = "2.0.0" 213 | source = "registry+https://github.com/rust-lang/crates.io-index" 214 | dependencies = [ 215 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 216 | "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", 217 | ] 218 | 219 | [[package]] 220 | name = "thread_local" 221 | version = "0.2.7" 222 | source = "registry+https://github.com/rust-lang/crates.io-index" 223 | dependencies = [ 224 | "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 225 | ] 226 | 227 | [[package]] 228 | name = "tokio-core" 229 | version = "0.1.7" 230 | source = "registry+https://github.com/rust-lang/crates.io-index" 231 | dependencies = [ 232 | "bytes 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", 233 | "futures 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", 234 | "iovec 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 235 | "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", 236 | "mio 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", 237 | "scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 238 | "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 239 | "tokio-io 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 240 | ] 241 | 242 | [[package]] 243 | name = "tokio-io" 244 | version = "0.1.1" 245 | source = "registry+https://github.com/rust-lang/crates.io-index" 246 | dependencies = [ 247 | "bytes 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", 248 | "futures 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", 249 | "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", 250 | ] 251 | 252 | [[package]] 253 | name = "unicode-segmentation" 254 | version = "1.1.0" 255 | source = "registry+https://github.com/rust-lang/crates.io-index" 256 | 257 | [[package]] 258 | name = "unicode-width" 259 | version = "0.1.4" 260 | source = "registry+https://github.com/rust-lang/crates.io-index" 261 | 262 | [[package]] 263 | name = "utf8-ranges" 264 | version = "0.1.3" 265 | source = "registry+https://github.com/rust-lang/crates.io-index" 266 | 267 | [[package]] 268 | name = "vec_map" 269 | version = "0.7.0" 270 | source = "registry+https://github.com/rust-lang/crates.io-index" 271 | 272 | [[package]] 273 | name = "winapi" 274 | version = "0.2.8" 275 | source = "registry+https://github.com/rust-lang/crates.io-index" 276 | 277 | [[package]] 278 | name = "winapi-build" 279 | version = "0.1.1" 280 | source = "registry+https://github.com/rust-lang/crates.io-index" 281 | 282 | [[package]] 283 | name = "ws2_32-sys" 284 | version = "0.2.1" 285 | source = "registry+https://github.com/rust-lang/crates.io-index" 286 | dependencies = [ 287 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 288 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 289 | ] 290 | 291 | [metadata] 292 | "checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66" 293 | "checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6" 294 | "checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159" 295 | "checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4" 296 | "checksum byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c40977b0ee6b9885c9013cd41d9feffdd22deb3bb4dc3a71d901cc7a77de18c8" 297 | "checksum bytes 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f9edb851115d67d1f18680f9326901768a91d37875b87015518357c6ce22b553" 298 | "checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" 299 | "checksum clap 2.24.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b7541069be0b8aec41030802abe8b5cdef0490070afaa55418adea93b1e431e0" 300 | "checksum env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "15abd780e45b3ea4f76b4e9a26ff4843258dd8a3eed2775a0e7368c2e7936c2f" 301 | "checksum futures 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "55f0008e13fc853f79ea8fc86e931486860d4c4c156cdffb59fa5f7fa833660a" 302 | "checksum iovec 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "29d062ee61fccdf25be172e70f34c9f6efc597e1fb8f6526e8437b2046ab26be" 303 | "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" 304 | "checksum lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce12306c4739d86ee97c23139f3a34ddf0387bbf181bc7929d287025a8c3ef6b" 305 | "checksum libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)" = "babb8281da88cba992fa1f4ddec7d63ed96280a1a53ec9b919fd37b53d71e502" 306 | "checksum log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "5141eca02775a762cc6cd564d8d2c50f67c0ea3a372cbf1c51592b3e029e10ad" 307 | "checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" 308 | "checksum mio 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "6d19442734abd7d780b981c590c325680d933e99795fe1f693f0686c9ed48022" 309 | "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" 310 | "checksum net2 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "bc01404e7568680f1259aa5729539f221cb1e6d047a0d9053cab4be8a73b5d67" 311 | "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" 312 | "checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957" 313 | "checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d" 314 | "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" 315 | "checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694" 316 | "checksum term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209" 317 | "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" 318 | "checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5" 319 | "checksum tokio-core 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "febd81b3e2ef615c6c8077347b33f3f3deec3d708ecd08194c9707b7a1eccfc9" 320 | "checksum tokio-io 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "48f55df1341bb92281f229a6030bc2abffde2c7a44c6d6b802b7687dd8be0775" 321 | "checksum unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18127285758f0e2c6cf325bb3f3d138a12fee27de4f23e146cd6a179f26c2cf3" 322 | "checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f" 323 | "checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" 324 | "checksum vec_map 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f8cdc8b93bd0198ed872357fb2e667f7125646b1762f16d60b2c96350d361897" 325 | "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" 326 | "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" 327 | "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" 328 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "mio-proxy-rs" 3 | version = "0.1.0" 4 | authors = ["Nerijus Arlauskas "] 5 | 6 | [dependencies] 7 | clap = "2.17.1" 8 | futures = "0.1" 9 | tokio-core = "0.1" 10 | env_logger = "0.3" 11 | log = "0.3" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Summary 2 | 3 | A simple TCP proxy server that forwards external traffic to specified internal port. 4 | 5 | __For up-to-date and correct version, [see official proxy example](https://github.com/tokio-rs/tokio-core/blob/master/examples/proxy.rs)__ -------------------------------------------------------------------------------- /src/error.rs: -------------------------------------------------------------------------------- 1 | use std::result; 2 | use std::net; 3 | use std::io; 4 | 5 | #[derive(Debug)] 6 | pub enum Error { 7 | AddrParseError(net::AddrParseError), 8 | Io(io::Error), 9 | } 10 | 11 | impl From for Error { 12 | fn from(other: net::AddrParseError) -> Self { 13 | Error::AddrParseError(other) 14 | } 15 | } 16 | 17 | impl From for Error { 18 | fn from(other: io::Error) -> Self { 19 | Error::Io(other) 20 | } 21 | } 22 | 23 | pub type Result = result::Result; -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate clap; 2 | extern crate futures; 3 | extern crate tokio_core; 4 | extern crate env_logger; 5 | #[macro_use] 6 | extern crate log; 7 | 8 | use clap::{Arg, App}; 9 | 10 | mod server; 11 | mod error; 12 | 13 | fn main() { 14 | env_logger::init().unwrap(); 15 | 16 | let matches = App::new("Mio Proxy") 17 | .version("1.0") 18 | .author("Nerijus Arlauskas ") 19 | .about("Proxies service from local port to another port") 20 | .arg(Arg::with_name("listen") 21 | .short("i") 22 | .long("listen") 23 | .help("sets the port to listen to") 24 | .multiple(false) 25 | .takes_value(true) 26 | .required(true)) 27 | .arg(Arg::with_name("forward") 28 | .short("o") 29 | .long("forward") 30 | .help("sets the forward address") 31 | .multiple(false) 32 | .takes_value(true) 33 | .required(true)) 34 | .get_matches(); 35 | 36 | let listen_port = matches.value_of("listen").expect("failed to get listen port"); 37 | let forward_to = matches.value_of("forward").expect("failed to get forward port"); 38 | 39 | server::run(listen_port.to_string(), forward_to.to_string()) 40 | .expect("server error!"); 41 | } -------------------------------------------------------------------------------- /src/server.rs: -------------------------------------------------------------------------------- 1 | use error::Result; 2 | 3 | use std::net::SocketAddr; 4 | 5 | use futures::{Future}; 6 | use futures::stream::Stream; 7 | use tokio_core::io::{copy, Io}; 8 | use tokio_core::net::{TcpListener, TcpStream}; 9 | use tokio_core::reactor::Core; 10 | 11 | pub fn run(listen_port: String, forward_to: String) -> Result<()> { 12 | let listen_addr = try!(format!("0.0.0.0:{}", listen_port).parse::()); 13 | let fwd_addr = try!(forward_to.parse::()); 14 | 15 | // Create the event loop that will drive this server 16 | let mut l = try!(Core::new()); 17 | let handle = l.handle(); 18 | 19 | // Create a TCP listener which will listen for incoming connections 20 | let accept_socket = try!(TcpListener::bind(&listen_addr, &handle)); 21 | 22 | let done = accept_socket.incoming().for_each(move |(server_socket, _addr)| { 23 | let client_pair = TcpStream::connect(&fwd_addr, &handle).map(|socket| socket.split()); 24 | 25 | // For up-to date and correct version, see 26 | // official example at https://github.com/tokio-rs/tokio-core/blob/master/examples/proxy.rs 27 | 28 | let amt = client_pair 29 | .and_then(move |(client_reader, client_writer)| { 30 | let (server_reader, server_writer) = server_socket.split(); 31 | let upload = copy(server_reader, client_writer); 32 | let download = copy(client_reader, server_writer); 33 | upload.join(download) 34 | }); 35 | 36 | let msg = amt.then(|res| { 37 | if let Ok((sent, received)) = res { 38 | println!("bytes sent {:?}, bytes received {:?}", sent, received); 39 | } 40 | Ok(()) 41 | }); 42 | 43 | handle.spawn(msg); 44 | 45 | Ok(()) 46 | }); 47 | 48 | try!(l.run(done)); 49 | 50 | Ok(()) 51 | } 52 | --------------------------------------------------------------------------------