Anyway to setsocket opts and then bind the outgoing connection to specific ip + port?

41 views
Skip to first unread message

Eliezer Croitoru

unread,
Sep 5, 2012, 3:29:05 AM9/5/12
to eventm...@googlegroups.com

I wrote a small server using eventmachine.
the problem is that I want to use linux TPROXY for an outgoing connection.

using raw tcpsockets I can select the destination then set the socket
opts for tproxy and connect.
example code for TPROXY client:

##start
#!/usr/bin/env ruby
require 'socket'
Socket::IP_TRANSPARENT = 19 # since ruby dosnt have this constant

sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
socksrcaddr = Socket.pack_sockaddr_in(3232, '127.0.0.7')

sockdstaddr = Socket.pack_sockaddr_in(3232, '127.0.0.1')
sock.setsockopt(Socket::SOL_IP, Socket::IP_TRANSPARENT, 1)

sock.bind(socksrcaddr)
sock.connect(sockdstaddr)
....
##end

a basic EM connect works with dst host + dst port + module\class

##start
EM::run do

bot = EM::connect $config_host, $config_port, Bot

bot.on_command_1 do |user, *args|
# handle command_1 in a block
end

# or if you want to modularize it, something like this
# where Command2Handler = lambda {|user, *args| #... }
bot.on_command_2(&Command2Handler)

end
##end

What I want to do is "EM::tproxy_connect $config_host, $config_port,
Bot, src_ip, src_port"
the src_ip and src_port should be nil by default and will be used in
either combination or just ip and the port will be randomized.

I have seen something about that there is a method to set the socket ops
and something about binding to specific ip and port but i'm kind of lost
it in the code.

Any help will be gladly welcomed

Thanks,
Eliezer

--
Eliezer Croitoru
https://www1.ngtech.co.il
IT consulting for Nonprofit organizations
eliezer <at> ngtech.co.il

Iñaki Baz Castillo

unread,
Sep 5, 2012, 7:09:47 AM9/5/12
to eventm...@googlegroups.com
2012/9/5 Eliezer Croitoru <eli...@ngtech.co.il>:
> I have seen something about that there is a method to set the socket ops and
> something about binding to specific ip and port but i'm kind of lost it in
> the code.

Usage example:


class MyTcpClient < EM::Connection

def post_init
set_sock_opt Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true
set_sock_opt Socket::SOL_TCP, Socket::TCP_KEEPIDLE, 30
set_sock_opt Socket::SOL_TCP, Socket::TCP_KEEPINTVL, 60
end

[...]

end


But it require EventMachine >= 1.0. I recommend you using EventMachine-LE.


--
Iñaki Baz Castillo
<i...@aliax.net>
Reply all
Reply to author
Forward
0 new messages