Created
June 9, 2021 15:43
-
-
Save kuredev/077244b441d4c5be5194fcae3920260d to your computer and use it in GitHub Desktop.
RubyでIP over UDPトンネリングを実現する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "socket" | |
require "rb_tuntap" | |
PEER = "" | |
PORT = 9876 | |
tun = RbTunTap::TunDevice.new("tun0") | |
tun.open(false) | |
tun.addr = "192.168.0.1" | |
tun.netmask = "255.255.255.0" | |
tun.up | |
tun_io = tun.to_io | |
sock = UDPSocket.open | |
sock.bind("0.0.0.0", 9876) | |
peer = Socket.pack_sockaddr_in(PORT, PEER) | |
while true | |
ret = IO::select([sock, tun_io]) # IOオブジェクト | |
ret[0].each do |d| | |
if d == tun_io | |
# TUNデバイスで受信した場合 | |
data = tun_io.sysread(65535) | |
sock.send(data, 0, peer) | |
else | |
# 実NWで受信した場合 | |
data = sock.recv(65535) | |
tun_io.syswrite(data) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PEER
を接続先のIPアドレスに書き換える% sudo ruby -rwebrick -e 'WEBrick::HTTPServer.new(:DocumentRoot => "./", :Port => 80, :BindAddress => "192.168.0.1").start'