aboutsummaryrefslogtreecommitdiff
path: root/plugins/socks.lua
blob: 2892e434c6040a98a8e9fa587ff801af83309817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
--
-- socks.lua: SOCKS service detection plugin
--
-- Copyright (c) 2015, Přemysl Eric Janouch <p@janouch.name>
--
-- Permission to use, copy, modify, and/or distribute this software for any
-- purpose with or without fee is hereby granted.
--
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
-- SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
-- OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
--

-- This plugin serves as an example of how to write Lua plugins for ponymap

ponymap.check_api_version (1)

-- SOCKS 4

local Socks4 = {}
Socks4.__index = Socks4

function Socks4.new (unit)
	unit:write (string.pack ("> I1 I1 I2 I1 I1 I1 I1 z",
		4, 1, 80, 127, 0, 0, 1, ""))
	return setmetatable ({ unit = unit, buf = "" }, Socks4)
end

function Socks4:on_data (data)
	self.buf = self.buf .. data
	if #self.buf >= 8 then
		null, code = string.unpack ("> I1 I1", self.buf)
		if null == 0 and code >= 90 and code <= 93 then
			self.unit:set_success (true)
		end
		self.unit:stop ()
	end
end

-- SOCKS 4A

local Socks4A = {}
Socks4A.__index = Socks4A

function Socks4A.new (unit)
	unit:write (string.pack ("> I1 I1 I2 I4 z z",
		4, 1, 80, 1, "", "google.com"))
	return setmetatable ({ unit = unit, buf = "" }, Socks4A)
end

Socks4A.on_data = Socks4.on_data

-- SOCKS 5

local Socks5 = {}
Socks5.__index = Socks5

function Socks5.new (unit)
	unit:write (string.pack ("> I1 I1 I1", 5, 1, 0))
	return setmetatable ({ unit = unit, buf = "" }, Socks5)
end

function Socks5:on_data (data)
	self.buf = self.buf .. data
	if #self.buf >= 2 then
		version, result = string.unpack ("> I1 I1", self.buf)
		if version == 5 and (result == 0 or result == 255) then
			if result == 0 then
				self.unit:add_info ("no authentication required")
			end
			self.unit:set_success (true)
		end
		self.unit:stop ()
	end
end

-- Register everything

ponymap.register_service { name="SOCKS4", flags=0, new_scan=Socks4.new }

-- At the moment this is nearly useless
-- ponymap.register_service { name="SOCKS4A", flags=0, new_scan=Socks4A.new }

ponymap.register_service { name="SOCKS5", flags=0, new_scan=Socks5.new }