A More Secure Internet Connection for Your Home https://fen.gg
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

372 lines
7.7 KiB

  1. package model
  2. //
  3. // Fengg Security Gateway Server Application
  4. // Copyright (C) 2020 Lukas Matt <support@fen.gg>
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. import (
  19. "time"
  20. "errors"
  21. "github.com/jmoiron/sqlx"
  22. _ "github.com/lib/pq"
  23. )
  24. type Protocol struct {
  25. ID uint `db:"id" json:"id"`
  26. CreatedAt time.Time `db:"created_at" json:"createdAt"`
  27. Name string `db:"name" json:"name"`
  28. }
  29. func NewProtocol() *Protocol {
  30. return &Protocol{CreatedAt: time.Now()}
  31. }
  32. func (protocol Protocol) Valid() bool {
  33. return len(protocol.Name) > 0 || protocol.ID > 0
  34. }
  35. func (protocol *Protocol) Find() error {
  36. if !protocol.Valid() {
  37. return errors.New("we need at least a Name or ID")
  38. }
  39. db, err := sqlx.Connect(dbDriver, dbConnect)
  40. if err != nil {
  41. return err
  42. }
  43. defer db.Close()
  44. if protocol.ID > 0 {
  45. return db.Get(protocol, "SELECT * FROM protocols WHERE id=$1;", protocol.ID)
  46. }
  47. return db.Get(protocol, "SELECT * FROM protocols WHERE name=$1;", protocol.Name)
  48. }
  49. func (protocol *Protocol) CreateIfNotExists() error {
  50. db, err := sqlx.Connect(dbDriver, dbConnect)
  51. if err != nil {
  52. return err
  53. }
  54. defer db.Close()
  55. err = db.Get(&(protocol.ID), `SELECT id FROM protocols WHERE name=$1 or id =$2;`,
  56. protocol.Name, protocol.ID)
  57. if err == nil {
  58. return nil
  59. }
  60. logger.Debug().Err(err).Msg("no protocol found")
  61. rows, err := db.NamedQuery(
  62. `INSERT INTO protocols (created_at, name) VALUES (now(), :name) RETURNING id`, protocol)
  63. if err != nil {
  64. return err
  65. }
  66. defer rows.Close()
  67. for rows.Next() {
  68. err = rows.Scan(&(protocol.ID))
  69. if err != nil {
  70. return err
  71. }
  72. }
  73. return nil
  74. }
  75. // String will print the string representation of the network protocol layer
  76. // (see https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml)
  77. func (protocol *Protocol) ParseIPHeader(num int) {
  78. switch num {
  79. case 0:
  80. protocol.Name = "HOPOPT"
  81. case 1:
  82. protocol.Name = "ICMPv4"
  83. case 2:
  84. protocol.Name = "IGMP"
  85. case 3:
  86. protocol.Name = "GGP"
  87. case 4:
  88. protocol.Name = "IPv4"
  89. case 5:
  90. protocol.Name = "ST"
  91. case 6:
  92. protocol.Name = "TCP"
  93. case 7:
  94. protocol.Name = "CBT"
  95. case 8:
  96. protocol.Name = "EGP"
  97. case 9:
  98. protocol.Name = "IGP"
  99. case 10:
  100. protocol.Name = "BBN-RCC-MON"
  101. case 11:
  102. protocol.Name = "NVP-II"
  103. case 12:
  104. protocol.Name = "PUP"
  105. case 13:
  106. protocol.Name = "ARGUS"
  107. case 14:
  108. protocol.Name = "EMCON"
  109. case 15:
  110. protocol.Name = "XNET"
  111. case 16:
  112. protocol.Name = "CHAOS"
  113. case 17:
  114. protocol.Name = "UDP"
  115. case 18:
  116. protocol.Name = "MUX"
  117. case 19:
  118. protocol.Name = "DCN-MEAS"
  119. case 20:
  120. protocol.Name = "HMP"
  121. case 21:
  122. protocol.Name = "PRM"
  123. case 22:
  124. protocol.Name = "XNS-IDP"
  125. case 23:
  126. protocol.Name = "TRUNK-1"
  127. case 24:
  128. protocol.Name = "TRUNK-2"
  129. case 25:
  130. protocol.Name = "LEAF-1"
  131. case 26:
  132. protocol.Name = "LEAF-2"
  133. case 27:
  134. protocol.Name = "RDP"
  135. case 28:
  136. protocol.Name = "IRTP"
  137. case 29:
  138. protocol.Name = "ISO-TP4"
  139. case 30:
  140. protocol.Name = "NETBLT"
  141. case 31:
  142. protocol.Name = "MFE-NSP"
  143. case 32:
  144. protocol.Name = "MERIT-INP"
  145. case 33:
  146. protocol.Name = "DCCP"
  147. case 34:
  148. protocol.Name = "3PC"
  149. case 35:
  150. protocol.Name = "IDPR"
  151. case 36:
  152. protocol.Name = "XTP"
  153. case 37:
  154. protocol.Name = "DDP"
  155. case 38:
  156. protocol.Name = "IDPR-CMTP"
  157. case 39:
  158. protocol.Name = "TP++"
  159. case 40:
  160. protocol.Name = "IL"
  161. case 41:
  162. protocol.Name = "IPv6"
  163. case 42:
  164. protocol.Name = "SDRP"
  165. case 43:
  166. protocol.Name = "Routev4"
  167. case 44:
  168. protocol.Name = "Fragv6"
  169. case 45:
  170. protocol.Name = "IDRP"
  171. case 46:
  172. protocol.Name = "RSVP"
  173. case 47:
  174. protocol.Name = "GRE"
  175. case 48:
  176. protocol.Name = "DSR"
  177. case 49:
  178. protocol.Name = "BNA"
  179. case 50:
  180. protocol.Name = "ESP"
  181. case 51:
  182. protocol.Name = "AH"
  183. case 52:
  184. protocol.Name = "I-NLSP"
  185. case 53:
  186. protocol.Name = "SWIPE"
  187. case 54:
  188. protocol.Name = "NARP"
  189. case 55:
  190. protocol.Name = "MOBILE"
  191. case 56:
  192. protocol.Name = "TLSP"
  193. case 57:
  194. protocol.Name = "SKIP"
  195. case 58:
  196. protocol.Name = "ICMPv6"
  197. case 59:
  198. protocol.Name = "NoNxtv6"
  199. case 60:
  200. protocol.Name = "Optsv6"
  201. case 62:
  202. protocol.Name = "CFTP"
  203. case 64:
  204. protocol.Name = "SAT-EXPAK"
  205. case 65:
  206. protocol.Name = "KRYPTOLAN"
  207. case 66:
  208. protocol.Name = "RVD"
  209. case 67:
  210. protocol.Name = "IPPC"
  211. case 69:
  212. protocol.Name = "SAT-MON"
  213. case 70:
  214. protocol.Name = "VISA"
  215. case 71:
  216. protocol.Name = "IPCV"
  217. case 72:
  218. protocol.Name = "CPNX"
  219. case 73:
  220. protocol.Name = "CPHB"
  221. case 74:
  222. protocol.Name = "WSN"
  223. case 75:
  224. protocol.Name = "PVP"
  225. case 76:
  226. protocol.Name = "BR-SAT-MON"
  227. case 77:
  228. protocol.Name = "SUN-ND"
  229. case 78:
  230. protocol.Name = "WB-MON"
  231. case 79:
  232. protocol.Name = "WB-EXPAK"
  233. case 80:
  234. protocol.Name = "ISO-IP"
  235. case 81:
  236. protocol.Name = "VMTP"
  237. case 82:
  238. protocol.Name = "SECURE-VMTP"
  239. case 83:
  240. protocol.Name = "VINES"
  241. case 84:
  242. protocol.Name = "TTP/IPTM"
  243. case 85:
  244. protocol.Name = "NSFNET-IGP"
  245. case 86:
  246. protocol.Name = "DGP"
  247. case 87:
  248. protocol.Name = "TCF"
  249. case 88:
  250. protocol.Name = "EIGRP"
  251. case 89:
  252. protocol.Name = "OSPFIGP"
  253. case 90:
  254. protocol.Name = "Sprite-RPC"
  255. case 91:
  256. protocol.Name = "LARP"
  257. case 92:
  258. protocol.Name = "MTP"
  259. case 93:
  260. protocol.Name = "AX.25"
  261. case 94:
  262. protocol.Name = "IPIP"
  263. case 95:
  264. protocol.Name = "MICP"
  265. case 96:
  266. protocol.Name = "SCC-SP"
  267. case 97:
  268. protocol.Name = "ETHERIP"
  269. case 98:
  270. protocol.Name = "ENCAP"
  271. case 100:
  272. protocol.Name = "GMTP"
  273. case 101:
  274. protocol.Name = "IFMP"
  275. case 102:
  276. protocol.Name = "PNNI"
  277. case 103:
  278. protocol.Name = "PIM"
  279. case 104:
  280. protocol.Name = "ARIS"
  281. case 105:
  282. protocol.Name = "SCPS"
  283. case 106:
  284. protocol.Name = "QNX"
  285. case 107:
  286. protocol.Name = "A/N"
  287. case 108:
  288. protocol.Name = "IPComp"
  289. case 109:
  290. protocol.Name = "SNP"
  291. case 110:
  292. protocol.Name = "Compaq-Peer"
  293. case 111:
  294. protocol.Name = "IPX-in-IP"
  295. case 112:
  296. protocol.Name = "VRRP"
  297. case 113:
  298. protocol.Name = "PGM"
  299. case 115:
  300. protocol.Name = "L2TP"
  301. case 116:
  302. protocol.Name = "DDX"
  303. case 117:
  304. protocol.Name = "IATP"
  305. case 118:
  306. protocol.Name = "STP"
  307. case 119:
  308. protocol.Name = "SRP"
  309. case 120:
  310. protocol.Name = "UTI"
  311. case 121:
  312. protocol.Name = "SMP"
  313. case 122:
  314. protocol.Name = "SM"
  315. case 123:
  316. protocol.Name = "PTP"
  317. case 124:
  318. protocol.Name = "ISIS"
  319. case 125:
  320. protocol.Name = "FIRE"
  321. case 126:
  322. protocol.Name = "CRTP"
  323. case 127:
  324. protocol.Name = "CRUDP"
  325. case 128:
  326. protocol.Name = "SSCOPMCE"
  327. case 129:
  328. protocol.Name = "IPLT"
  329. case 130:
  330. protocol.Name = "SPS"
  331. case 131:
  332. protocol.Name = "PIPE"
  333. case 132:
  334. protocol.Name = "SCTP"
  335. case 133:
  336. protocol.Name = "FC"
  337. case 134:
  338. protocol.Name = "RSVP-E2E-IGNORE"
  339. case 135:
  340. protocol.Name = "Mobility"
  341. case 136:
  342. protocol.Name = "UDPLite"
  343. case 137:
  344. protocol.Name = "MPLS-in-IP"
  345. case 138:
  346. protocol.Name = "manet"
  347. case 139:
  348. protocol.Name = "HIP"
  349. case 140:
  350. protocol.Name = "Shim6"
  351. case 141:
  352. protocol.Name = "WESP"
  353. case 142:
  354. protocol.Name = "ROHC"
  355. default:
  356. protocol.Name = "UNKNOWN"
  357. }
  358. }