Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RubyでIP over UDPトンネリングを実現する
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
@kuredev

This comment has been minimized.

Copy link
Owner Author

@kuredev kuredev commented Jun 9, 2021

  • ピア同士で実行する
  • PEER を接続先のIPアドレスに書き換える
  • tun.addr の部分は違うアドレスを指定する
  • 受付用のアプリをTUNデバイスにバインド(Webrickだったら↓)
  • PEER同士で実行した後、クライアント側でwget 192.168.0.1
% sudo ruby -rwebrick -e 'WEBrick::HTTPServer.new(:DocumentRoot => "./", :Port => 80, :BindAddress => "192.168.0.1").start'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment