ip_change.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. import ctypes
  2. from copy import deepcopy
  3. from datetime import datetime
  4. from random import choice, randint
  5. import pytz
  6. import requests
  7. from apscheduler.schedulers.blocking import BlockingScheduler
  8. URL = 'http://192.168.88.1/jsproxy'
  9. HEADERS = {
  10. 'Accept-Language': '',
  11. 'Content-Type': 'msg',
  12. 'Cookie': 'username=root',
  13. }
  14. def int_overflow(val) -> int:
  15. maxint = 2147483647
  16. if not -maxint - 1 <= val <= maxint:
  17. val = (val + (maxint + 1)) % (2 * (maxint + 1)) - maxint - 1
  18. return val & 0xFFFFFFFF
  19. def unsigned_right_shift(n, i) -> int:
  20. """实现无符号右移"""
  21. if n < 0:
  22. n = ctypes.c_uint32(n).value
  23. if i < 0:
  24. return -int_overflow(n << abs(i))
  25. return int_overflow(n >> i)
  26. class Buffer(object):
  27. def __init__(self) -> None:
  28. self.MASK_FTYPE = 0xf8000000
  29. self.FT_BOOL = int_overflow(0 << 27)
  30. self.FT_U32 = int_overflow(1 << 27)
  31. self.FT_U64 = int_overflow(2 << 27)
  32. self.FT_ADDR6 = int_overflow(3 << 27)
  33. self.FT_STRING = int_overflow(4 << 27)
  34. self.FT_MESSAGE = int_overflow(5 << 27)
  35. self.FT_RAW = int_overflow(6 << 27)
  36. self.FT_BOOL_ARRAY = int_overflow(16 << 27)
  37. self.FT_U32_ARRAY = int_overflow(17 << 27)
  38. self.FT_U64_ARRAY = int_overflow(18 << 27)
  39. self.FT_ADDR6_ARRAY = int_overflow(19 << 27)
  40. self.FT_STRING_ARRAY = int_overflow(20 << 27)
  41. self.FT_MESSAGE_ARRAY = int_overflow(21 << 27)
  42. self.FT_RAW_ARRAY = int_overflow(22 << 27)
  43. self.FS_SHORT = int_overflow(1 << 24)
  44. self.arr = [0] * 64 * 1024
  45. self.pos = 0
  46. def msg2buffer(self, msg):
  47. self.arr[self.pos] = 0x4d
  48. self.pos += 1
  49. self.arr[self.pos] = 0x32
  50. self.pos += 1
  51. for r in msg:
  52. pfx = r[0]
  53. if pfx == '_':
  54. continue
  55. val = msg[r]
  56. match pfx:
  57. case 'b':
  58. self.write_id(self.FT_BOOL | (
  59. self.FS_SHORT if val else 0), r)
  60. case 'u':
  61. val = val if val is not None else -1
  62. if 0 <= val < 256:
  63. self.write_id(self.FT_U32 | self.FS_SHORT, r)
  64. self.arr[self.pos] = val
  65. self.pos += 1
  66. else:
  67. self.write_id(self.FT_U32, r)
  68. self.write_32(val)
  69. case 'q':
  70. self.write_id(self.FT_U64, r)
  71. self.write_64(val)
  72. case 'a':
  73. self.write_id(self.FT_ADDR6, r)
  74. for i in range(16):
  75. self.arr[self.pos] = val[i]
  76. self.pos += 1
  77. case 's':
  78. if len(val) < 256:
  79. self.write_id(self.FT_STRING | self.FS_SHORT, r)
  80. self.arr[self.pos] = len(val)
  81. self.pos += 1
  82. else:
  83. self.write_id(self.FT_STRING, r)
  84. self.write_16(len(val))
  85. for i in range(len(val)):
  86. self.arr[self.pos] = ord(val[i])
  87. self.pos += 1
  88. case 'r':
  89. if len(val) < 256:
  90. self.write_id(self.FT_RAW | self.FS_SHORT, r)
  91. self.arr[self.pos] = len(val)
  92. self.pos += 1
  93. else:
  94. self.write_id(self.FT_RAW, r)
  95. self.write_16(len(val))
  96. for i in range(len(val)):
  97. self.arr[self.pos] = val[i]
  98. self.pos += 1
  99. case 'm':
  100. x = self.msg2buffer(val)
  101. if len(x) < 256:
  102. self.write_id(self.FT_MESSAGE | self.FS_SHORT, r)
  103. self.arr[self.pos] = len(x)
  104. self.pos += 1
  105. else:
  106. self.write_id(self.FT_MESSAGE, r)
  107. self.write_16(len(x))
  108. for item in x[::-1]:
  109. self.arr[self.pos] = item
  110. self.pos += len(x)
  111. case 'B':
  112. self.write_id(self.FT_BOOL_ARRAY, r)
  113. self.write_16(len(val))
  114. for i in range(len(val)):
  115. self.arr[self.pos] = val[i]
  116. self.pos += 1
  117. case 'U':
  118. self.write_id(self.FT_U32_ARRAY, r)
  119. self.write_16(len(val))
  120. for i in range(len(val)):
  121. self.write_32(val[i])
  122. case 'Q':
  123. self.write_id(self.FT_U64_ARRAY, r)
  124. self.write_16(len(val))
  125. for i in range(len(val)):
  126. self.write_64(val[i])
  127. case 'A':
  128. self.write_id(self.FT_ADDR6_ARRAY, r)
  129. self.write_16(len(val))
  130. for i in range(len(val)):
  131. for k in range(16):
  132. self.arr[self.pos] = val[i][k]
  133. self.pos += 1
  134. case 'S':
  135. self.write_id(self.FT_STRING_ARRAY, r)
  136. self.write_16(len(val))
  137. for i in range(len(val)):
  138. self.write_16(len(val[i]))
  139. for k in range(len(val[i])):
  140. self.arr[self.pos] = ord(val[i][k])
  141. self.pos += 1
  142. case 'R':
  143. self.write_id(self.FT_RAW_ARRAY, r)
  144. self.write_16(len(val))
  145. for i in range(len(val)):
  146. self.write_16(len(val[i]))
  147. for k in range(len(val[i])):
  148. self.arr[self.pos] = val[i][k]
  149. self.pos += 1
  150. case 'M':
  151. self.write_id(self.FT_MESSAGE_ARRAY, r)
  152. self.write_16(len(val))
  153. for i in range(len(val)):
  154. x = self.msg2buffer(val[i])
  155. self.write_16(len(x))
  156. for item in x[::-1]:
  157. self.arr[self.pos] = item
  158. self.pos += len(x)
  159. case _:
  160. return None
  161. return self.arr[:self.pos]
  162. def buffer2msg(self, arr, offset: int = 0):
  163. self.arr, self.pos, ret = arr, 2, dict()
  164. if self.arr[0] != 0x4d or self.arr[1] != 0x32:
  165. return ret
  166. while self.pos < len(self.arr):
  167. _id = self.read_32()
  168. match _id & self.MASK_FTYPE:
  169. case self.FT_BOOL:
  170. ret['b' + self.idnum2hex(_id)] = 1 if (_id & self.FS_SHORT) else 0
  171. case self.FT_U32:
  172. if _id & self.FS_SHORT:
  173. ret['u' + self.idnum2hex(_id)] = self.arr[self.pos]
  174. self.pos += 1
  175. else:
  176. ret['u' + self.idnum2hex(_id)
  177. ] = self.int2num(self.read_32())
  178. case self.FT_U64:
  179. ret['q' + self.idnum2hex(_id)] = self.read_64()
  180. case self.FT_ADDR6:
  181. a = []
  182. for i in range(16):
  183. a[i] = self.arr[self.pos]
  184. self.pos += 1
  185. ret['a' + self.idnum2hex(_id)] = a
  186. case self.FT_STRING:
  187. length = self.arr[self.pos]
  188. self.pos += 1
  189. if not (_id & self.FS_SHORT):
  190. length |= self.arr[self.pos] << 8
  191. self.pos += 1
  192. s = ''
  193. for i in range(length):
  194. s = s + chr(self.arr[self.pos])
  195. self.pos += 1
  196. ret['s' + self.idnum2hex(_id)] = s
  197. case self.FT_RAW:
  198. length = self.arr[self.pos]
  199. self.pos += 1
  200. if not (_id & self.FS_SHORT):
  201. length |= self.arr[self.pos] << 8
  202. self.pos += 1
  203. a = [0] * length
  204. for i in range(length):
  205. a[i] = self.arr[self.pos]
  206. self.pos += 1
  207. ret['r' + self.idnum2hex(_id)] = a
  208. case self.FT_MESSAGE:
  209. length = self.arr[self.pos]
  210. self.pos += 1
  211. if not (_id & self.FS_SHORT):
  212. length |= self.arr[self.pos] << 8
  213. self.pos += 1
  214. ret['m' + self.idnum2hex(_id)] = self.buffer2msg(
  215. self.arr[offset + self.pos:offset + self.pos + length])
  216. self.pos += length
  217. offset += self.pos
  218. case self.FT_BOOL_ARRAY:
  219. length = self.read_16()
  220. a = [0] * length
  221. for i in range(length):
  222. a[i] = not (not self.arr[self.pos])
  223. self.pos += 1
  224. ret['B' + self.idnum2hex(_id)] = a
  225. case self.FT_U32_ARRAY:
  226. length = self.read_16()
  227. a = [0] * length
  228. for i in range(length):
  229. a[i] = self.int2num(self.read_32())
  230. ret['U' + self.idnum2hex(_id)] = a
  231. case self.FT_U64_ARRAY:
  232. length = self.read_16()
  233. a = [0] * length
  234. for i in range(length):
  235. a[i] = self.read_64()
  236. ret['Q' + self.idnum2hex(_id)] = a
  237. case self.FT_ADDR6_ARRAY:
  238. length = self.read_16()
  239. a = [0] * length
  240. for i in range(length):
  241. x = [0] * 16
  242. for k in range(16):
  243. x[k] = self.arr[self.pos]
  244. self.pos += 1
  245. a[i] = x
  246. ret['A' + self.idnum2hex(_id)] = a
  247. case self.FT_STRING_ARRAY:
  248. length = self.read_16()
  249. a = [0] * length
  250. for i in range(length):
  251. x = ''
  252. x_len = self.read_16()
  253. for k in range(x_len):
  254. x = x + chr(self.arr[self.pos])
  255. self.pos += 1
  256. a[i] = x
  257. ret['S' + self.idnum2hex(_id)] = a
  258. case self.FT_RAW_ARRAY:
  259. length = self.read_16()
  260. a = [0] * length
  261. for i in range(length):
  262. x_len = self.read_16()
  263. x = [0] * x_len
  264. for k in range(x_len):
  265. x[k] = self.arr[self.pos]
  266. self.pos += 1
  267. a[i] = x
  268. ret['R' + self.idnum2hex(_id)] = a
  269. case self.FT_MESSAGE_ARRAY:
  270. length = self.read_16()
  271. a = [0] * length
  272. for i in range(length):
  273. x_len = self.read_16()
  274. a[i] = self.buffer2msg(self.arr[offset + self.pos:offset + self.pos + x_len], offset + self.pos)
  275. self.pos += x_len
  276. offset += self.pos
  277. ret['M' + self.idnum2hex(_id)] = a
  278. return ret
  279. def buffer2msgs(self, arr, offset: int = 0):
  280. ret, pos = [], 0
  281. while pos + 2 <= len(arr):
  282. length = (arr[pos] << 8) | arr[pos + 1]
  283. arr[pos] = 0x4d
  284. arr[pos + 1] = 0x32
  285. msg = self.buffer2msg(arr[:offset + pos + length], offset + pos)
  286. pos += length
  287. ret.append(msg)
  288. return ret
  289. def write_id(self, id_type, id_str):
  290. x = int(id_str[1:], 16)
  291. self.arr[self.pos] = x & 0xff
  292. self.pos += 1
  293. self.arr[self.pos] = (x >> 8) & 0xff
  294. self.pos += 1
  295. self.arr[self.pos] = (x >> 16) & 0xff
  296. self.pos += 1
  297. self.arr[self.pos] = (id_type >> 24) & 0xff
  298. self.pos += 1
  299. def write_16(self, val):
  300. self.arr[self.pos] = val & 0xff
  301. self.pos += 1
  302. self.arr[self.pos] = (val >> 8) & 0xff
  303. self.pos += 1
  304. def write_32(self, val):
  305. for i in range(4):
  306. self.arr[self.pos] = (val >> (i * 8)) & 0xff
  307. self.pos += 1
  308. def write_64(self, val):
  309. for i in range(4):
  310. self.arr[self.pos] = (val >> (i * 8)) & 0xff
  311. self.pos += 1
  312. temp = int(val / 4294967296)
  313. for i in range(4):
  314. self.arr[self.pos] = (temp >> (i * 8)) & 0xff
  315. self.pos += 1
  316. def num2hex(self, ccc):
  317. if ccc < 10:
  318. return chr(ccc + 48)
  319. return chr(ccc + 87)
  320. def idnum2hex(self, _id):
  321. ret = ''
  322. for i in range(6):
  323. x = (_id >> (20 - (i * 4))) & 0xf
  324. if len(ret) == 0 and not x:
  325. continue
  326. ret = ret + self.num2hex(x)
  327. if len(ret) == 0:
  328. ret = '0'
  329. return ret
  330. def read_16(self):
  331. ret = 0
  332. for i in range(2):
  333. ret |= int_overflow(self.arr[self.pos] << (i * 8))
  334. self.pos += 1
  335. return ret
  336. def read_32(self):
  337. ret = 0
  338. for i in range(4):
  339. ret |= int_overflow(self.arr[self.pos] << (i * 8))
  340. self.pos += 1
  341. return ret
  342. def read_64(self):
  343. ret = 0
  344. for i in range(4):
  345. ret |= int_overflow(self.arr[self.pos] << (i * 8))
  346. self.pos += 1
  347. temp = 0
  348. for i in range(4):
  349. temp |= int_overflow(self.arr[self.pos] << (i * 8))
  350. self.pos += 1
  351. return self.int2num(ret) + temp * 4294967296
  352. def int2num(self, v):
  353. return 0x100000000 + v if v < 0 else v
  354. class Curve(object):
  355. @classmethod
  356. def curve_a2u(cls, a):
  357. r = [0] * 32
  358. for i in range(32):
  359. r[i >> 1] |= a[31 - i] << (i & 1) * 8
  360. return r
  361. @classmethod
  362. def curve_u2a(cls, a):
  363. r = [0] * 32
  364. for i in range(32):
  365. r[31 - i] = (a[i >> 1] >> ((i & 1) * 8)) & 0xff
  366. return r
  367. @classmethod
  368. def byte2str(cls, b):
  369. b &= 0xff
  370. return chr(b if b else 256)
  371. @classmethod
  372. def word2str(cls, w):
  373. return cls.byte2str(w >> 24) + cls.byte2str(w >> 16) + cls.byte2str(w >> 8) + cls.byte2str(w)
  374. @classmethod
  375. def str2byte(cls, s, off):
  376. return s[off] & 0xff
  377. @classmethod
  378. def str2word(cls, s, off):
  379. return int_overflow(cls.str2byte(s, off) << 24) | int_overflow(cls.str2byte(s, off + 1) << 16) | int_overflow(
  380. cls.str2byte(s, off + 2) << 8) | int_overflow(cls.str2byte(s, off + 3))
  381. @classmethod
  382. def str2a(cls, s):
  383. res = []
  384. for i in range(len(s)):
  385. res.append(s[i] & 0xff)
  386. return res
  387. @classmethod
  388. def a2str(cls, a):
  389. x = []
  390. for i in range(len(a)):
  391. x.append(cls.byte2str(a[i]))
  392. return ''.join(x)
  393. @classmethod
  394. def c255lgetbit(cls, n, c):
  395. return (n[c >> 4] >> (c & 0xf)) & 1
  396. @classmethod
  397. def c255lzero(cls):
  398. return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  399. @classmethod
  400. def c255lone(cls):
  401. return [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  402. @classmethod
  403. def c255lbase(cls):
  404. return [9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  405. @classmethod
  406. def c255lsqr8h(cls, a7, a6, a5, a4, a3, a2, a1, a0):
  407. r = [0] * 16
  408. v = a0 * a0
  409. r[0] = v & 0xffff
  410. v = (0 | int(v / 0x10000)) + 2 * a0 * a1
  411. r[1] = v & 0xffff
  412. v = (0 | int(v / 0x10000)) + 2 * a0 * a2 + a1 * a1
  413. r[2] = v & 0xffff
  414. v = (0 | int(v / 0x10000)) + 2 * a0 * a3 + 2 * a1 * a2
  415. r[3] = v & 0xffff
  416. v = (0 | int(v / 0x10000)) + 2 * a0 * a4 + 2 * a1 * a3 + a2 * a2
  417. r[4] = v & 0xffff
  418. v = (0 | int(v / 0x10000)) + 2 * a0 * a5 + 2 * a1 * a4 + 2 * a2 * a3
  419. r[5] = v & 0xffff
  420. v = (0 | int(v / 0x10000)) + 2 * a0 * a6 + \
  421. 2 * a1 * a5 + 2 * a2 * a4 + a3 * a3
  422. r[6] = v & 0xffff
  423. v = (0 | int(v / 0x10000)) + 2 * a0 * a7 + \
  424. 2 * a1 * a6 + 2 * a2 * a5 + 2 * a3 * a4
  425. r[7] = v & 0xffff
  426. v = (0 | int(v / 0x10000)) + 2 * a1 * a7 + \
  427. 2 * a2 * a6 + 2 * a3 * a5 + a4 * a4
  428. r[8] = v & 0xffff
  429. v = (0 | int(v / 0x10000)) + 2 * a2 * a7 + 2 * a3 * a6 + 2 * a4 * a5
  430. r[9] = v & 0xffff
  431. v = (0 | int(v / 0x10000)) + 2 * a3 * a7 + 2 * a4 * a6 + a5 * a5
  432. r[10] = v & 0xffff
  433. v = (0 | int(v / 0x10000)) + 2 * a4 * a7 + 2 * a5 * a6
  434. r[11] = v & 0xffff
  435. v = (0 | int(v / 0x10000)) + 2 * a5 * a7 + a6 * a6
  436. r[12] = v & 0xffff
  437. v = (0 | int(v / 0x10000)) + 2 * a6 * a7
  438. r[13] = v & 0xffff
  439. v = (0 | int(v / 0x10000)) + a7 * a7
  440. r[14] = v & 0xffff
  441. r[15] = 0 | int(v / 0x10000)
  442. return r
  443. @classmethod
  444. def c255lsqrmodp(cls, a):
  445. x = cls.c255lsqr8h(a[15], a[14], a[13], a[12],
  446. a[11], a[10], a[9], a[8])
  447. z = cls.c255lsqr8h(a[7], a[6], a[5], a[4], a[3], a[2], a[1], a[0])
  448. y = cls.c255lsqr8h(a[15] + a[7], a[14] + a[6], a[13] + a[5], a[12] +
  449. a[4], a[11] + a[3], a[10] + a[2], a[9] + a[1], a[8] + a[0])
  450. r = [0] * 16
  451. v = 0x800000 + z[0] + (y[8] - x[8] - z[8] + x[0] - 0x80) * 38
  452. r[0] = v & 0xffff
  453. for i in range(1, 8):
  454. v = 0x7fff80 + \
  455. unsigned_right_shift(
  456. v, 16) + z[i] + (y[i + 8] - x[i + 8] - z[i + 8] + x[i]) * 38
  457. r[i] = v & 0xffff
  458. for i in range(8, 15):
  459. v = 0x7fff80 + \
  460. unsigned_right_shift(
  461. v, 16) + z[i] + y[i - 8] - x[i - 8] - z[i - 8] + x[i] * 38
  462. r[i] = v & 0xffff
  463. r[15] = 0x7fff80 + \
  464. unsigned_right_shift(v, 16) + \
  465. z[15] + y[7] - x[7] - z[7] + x[15] * 38
  466. cls.c255lreduce(r)
  467. return r
  468. @classmethod
  469. def c255lmul8h(cls, a7, a6, a5, a4, a3, a2, a1, a0, b7, b6, b5, b4, b3, b2, b1, b0):
  470. r = [0] * 16
  471. v = a0 * b0
  472. r[0] = v & 0xffff
  473. v = (0 | int(v / 0x10000)) + a0 * b1 + a1 * b0
  474. r[1] = v & 0xffff
  475. v = (0 | int(v / 0x10000)) + a0 * b2 + a1 * b1 + a2 * b0
  476. r[2] = v & 0xffff
  477. v = (0 | int(v / 0x10000)) + a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0
  478. r[3] = v & 0xffff
  479. v = (0 | int(v / 0x10000)) + a0 * b4 + \
  480. a1 * b3 + a2 * b2 + a3 * b1 + a4 * b0
  481. r[4] = v & 0xffff
  482. v = (0 | int(v / 0x10000)) + a0 * b5 + a1 * \
  483. b4 + a2 * b3 + a3 * b2 + a4 * b1 + a5 * b0
  484. r[5] = v & 0xffff
  485. v = (0 | int(v / 0x10000)) + a0 * b6 + a1 * b5 + \
  486. a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 + a6 * b0
  487. r[6] = v & 0xffff
  488. v = (0 | int(v / 0x10000)) + a0 * b7 + a1 * b6 + a2 * \
  489. b5 + a3 * b4 + a4 * b3 + a5 * b2 + a6 * b1 + a7 * b0
  490. r[7] = v & 0xffff
  491. v = (0 | int(v / 0x10000)) + a1 * b7 + a2 * b6 + \
  492. a3 * b5 + a4 * b4 + a5 * b3 + a6 * b2 + a7 * b1
  493. r[8] = v & 0xffff
  494. v = (0 | int(v / 0x10000)) + a2 * b7 + a3 * \
  495. b6 + a4 * b5 + a5 * b4 + a6 * b3 + a7 * b2
  496. r[9] = v & 0xffff
  497. v = (0 | int(v / 0x10000)) + a3 * b7 + \
  498. a4 * b6 + a5 * b5 + a6 * b4 + a7 * b3
  499. r[10] = v & 0xffff
  500. v = (0 | int(v / 0x10000)) + a4 * b7 + a5 * b6 + a6 * b5 + a7 * b4
  501. r[11] = v & 0xffff
  502. v = (0 | int(v / 0x10000)) + a5 * b7 + a6 * b6 + a7 * b5
  503. r[12] = v & 0xffff
  504. v = (0 | int(v / 0x10000)) + a6 * b7 + a7 * b6
  505. r[13] = v & 0xffff
  506. v = (0 | int(v / 0x10000)) + a7 * b7
  507. r[14] = v & 0xffff
  508. r[15] = 0 | int(v / 0x10000)
  509. return r
  510. @classmethod
  511. def c255lmulmodp(cls, a, b):
  512. x = cls.c255lmul8h(a[15], a[14], a[13], a[12], a[11], a[10], a[9],
  513. a[8], b[15], b[14], b[13], b[12], b[11], b[10], b[9], b[8])
  514. z = cls.c255lmul8h(a[7], a[6], a[5], a[4], a[3], a[2], a[1],
  515. a[0], b[7], b[6], b[5], b[4], b[3], b[2], b[1], b[0])
  516. y = cls.c255lmul8h(a[15] + a[7], a[14] + a[6], a[13] + a[5], a[12] + a[4], a[11] + a[3], a[10] + a[2],
  517. a[9] + a[1], a[8] +
  518. a[0], b[15] + b[7], b[14] + b[6], b[13] + b[5], b[12] + b[4], b[11] + b[3], b[10] + b[2],
  519. b[9] + b[1], b[8] + b[0])
  520. r = [0] * 16
  521. v = 0x800000 + z[0] + (y[8] - x[8] - z[8] + x[0] - 0x80) * 38
  522. r[0] = v & 0xffff
  523. for i in range(1, 8):
  524. v = 0x7fff80 + \
  525. unsigned_right_shift(
  526. v, 16) + z[i] + (y[i + 8] - x[i + 8] - z[i + 8] + x[i]) * 38
  527. r[i] = v & 0xffff
  528. for i in range(8, 15):
  529. v = 0x7fff80 + \
  530. unsigned_right_shift(
  531. v, 16) + z[i] + y[i - 8] - x[i - 8] - z[i - 8] + x[i] * 38
  532. r[i] = v & 0xffff
  533. r[15] = 0x7fff80 + \
  534. unsigned_right_shift(v, 16) + \
  535. z[15] + y[7] - x[7] - z[7] + x[15] * 38
  536. cls.c255lreduce(r)
  537. return r
  538. @classmethod
  539. def c255lreduce(cls, a):
  540. v = a[15]
  541. a[15] = v & 0x7fff
  542. v = (0 | int(v / 0x8000)) * 19
  543. for i in range(15):
  544. v += a[i]
  545. a[i] = v & 0xffff
  546. v = unsigned_right_shift(v, 16)
  547. a[15] += v
  548. @classmethod
  549. def c255laddmodp(cls, a, b):
  550. r = [0] * 16
  551. v = ((0 | unsigned_right_shift(
  552. a[15], 15)) + (0 | unsigned_right_shift(b[15], 15))) * 19 + a[0] + b[0]
  553. r[0] = v & 0xffff
  554. for i in range(1, 15):
  555. v = unsigned_right_shift(v, 16) + a[i] + b[i]
  556. r[i] = v & 0xffff
  557. r[15] = unsigned_right_shift(
  558. v, 16) + (a[15] & 0x7fff) + (b[15] & 0x7fff)
  559. return r
  560. @classmethod
  561. def c255lsubmodp(cls, a, b):
  562. r = [0] * 16
  563. v = 0x80000 + ((0 | unsigned_right_shift(a[15], 15)) - (
  564. 0 | unsigned_right_shift(b[15], 15)) - 1) * 19 + a[0] - b[0]
  565. r[0] = v & 0xffff
  566. for i in range(1, 15):
  567. v = unsigned_right_shift(v, 16) + 0x7fff8 + a[i] - b[i]
  568. r[i] = v & 0xffff
  569. r[15] = unsigned_right_shift(
  570. v, 16) + 0x7ff8 + (a[15] & 0x7fff) - (b[15] & 0x7fff)
  571. return r
  572. @classmethod
  573. def c255linvmodp(cls, a):
  574. c, i = a, 249
  575. while i > 0:
  576. i -= 1
  577. a = cls.c255lsqrmodp(a)
  578. a = cls.c255lmulmodp(a, c)
  579. a = cls.c255lsqrmodp(a)
  580. a = cls.c255lsqrmodp(a)
  581. a = cls.c255lmulmodp(a, c)
  582. a = cls.c255lsqrmodp(a)
  583. a = cls.c255lsqrmodp(a)
  584. a = cls.c255lmulmodp(a, c)
  585. a = cls.c255lsqrmodp(a)
  586. a = cls.c255lmulmodp(a, c)
  587. return a
  588. @classmethod
  589. def c255lmulasmall(cls, a):
  590. m, r = 121665, [0] * 16
  591. v = a[0] * m
  592. r[0] = v & 0xffff
  593. for i in range(1, 15):
  594. v = (0 | int(v / 0x10000)) + a[i] * m
  595. r[i] = v & 0xffff
  596. r[15] = (0 | int(v / 0x10000)) + a[15] * m
  597. cls.c255lreduce(r)
  598. return r
  599. @classmethod
  600. def c255ldbl(cls, x, z):
  601. m = cls.c255lsqrmodp(cls.c255laddmodp(x, z))
  602. n = cls.c255lsqrmodp(cls.c255lsubmodp(x, z))
  603. o = cls.c255lsubmodp(m, n)
  604. x_2 = cls.c255lmulmodp(n, m)
  605. z_2 = cls.c255lmulmodp(cls.c255laddmodp(cls.c255lmulasmall(o), m), o)
  606. return [x_2, z_2]
  607. @classmethod
  608. def c255lsum(cls, x, z, x_p, z_p, x_1):
  609. p = cls.c255lmulmodp(cls.c255lsubmodp(
  610. x, z), cls.c255laddmodp(x_p, z_p))
  611. q = cls.c255lmulmodp(cls.c255laddmodp(
  612. x, z), cls.c255lsubmodp(x_p, z_p))
  613. x_3 = cls.c255lsqrmodp(cls.c255laddmodp(p, q))
  614. z_3 = cls.c255lmulmodp(cls.c255lsqrmodp(cls.c255lsubmodp(p, q)), x_1)
  615. return [x_3, z_3]
  616. @classmethod
  617. def curve25519_raw(cls, f, c):
  618. x_1 = c
  619. a = cls.c255ldbl(x_1, cls.c255lone())
  620. q = [deepcopy(x_1), cls.c255lone()]
  621. n = 255
  622. while cls.c255lgetbit(f, n) == 0:
  623. n -= 1
  624. if n < 0:
  625. return cls.c255lzero()
  626. n -= 1
  627. while n >= 0:
  628. b = cls.c255lgetbit(f, n)
  629. a_or_q = [[0] * 16, [0] * 16]
  630. cls.cond_copy(a_or_q[0], q[0], a[0], b)
  631. cls.cond_copy(a_or_q[1], q[1], a[1], b)
  632. r = cls.c255lsum(a[0], a[1], q[0], q[1], x_1)
  633. s = cls.c255ldbl(a_or_q[0], a_or_q[1])
  634. cls.cond_copy(q[0], s[0], r[0], b)
  635. cls.cond_copy(q[1], s[1], r[1], b)
  636. cls.cond_copy(a[0], r[0], s[0], b)
  637. cls.cond_copy(a[1], r[1], s[1], b)
  638. n -= 1
  639. q[1] = cls.c255linvmodp(q[1])
  640. q[0] = cls.c255lmulmodp(q[0], q[1])
  641. cls.c255lreduce(q[0])
  642. return q[0]
  643. @classmethod
  644. def cond_copy(cls, r, a, b, c):
  645. m2 = (-c) & 0xffff
  646. m1 = (~m2) & 0xffff
  647. n = 0
  648. while n < 16:
  649. r[n] = (a[n] & m1) | (b[n] & m2)
  650. n += 1
  651. @classmethod
  652. def curve25519(cls, f, c: list = None):
  653. if not c:
  654. c = cls.c255lbase()
  655. f[0] &= 0xFFF8
  656. f[15] = (f[15] & 0x7FFF) | 0x4000
  657. c[15] &= 0x7FFF
  658. return cls.curve25519_raw(f, c)
  659. @classmethod
  660. def sha1(cls, msg):
  661. length = len(msg)
  662. total_length = length + 9
  663. total_length = (total_length + 63) & -64
  664. padding = [0x80]
  665. padding.extend([0 for _ in range(length + 1, total_length)])
  666. msg.extend(padding)
  667. cls.packbe(msg, total_length - 4, length * 8)
  668. h0, h1, h2, h3, h4, w = 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, [
  669. 0] * 80
  670. for j in range(0, len(msg), 64):
  671. for i in range(16):
  672. w[i] = int_overflow(cls.unpackbe(msg, j + i * 4))
  673. for i in range(16, 80):
  674. w[i] = int_overflow(cls.rrotate(
  675. w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 31))
  676. a, b, c, d, e = h0, h1, h2, h3, h4
  677. for i in range(80):
  678. if i < 20:
  679. f = int_overflow((b & c) | (~b & d))
  680. k = 0x5A827999
  681. elif i < 40:
  682. f = int_overflow(b ^ c ^ d)
  683. k = 0x6ED9EBA1
  684. elif i < 60:
  685. f = int_overflow((b & c) | (b & d) | (c & d))
  686. k = 0x8F1BBCDC
  687. else:
  688. f = int_overflow(b ^ c ^ d)
  689. k = 0xCA62C1D6
  690. t = int_overflow(Curve.rrotate(a, 27) + f + e + k + w[i])
  691. e = d
  692. d = c
  693. c = int_overflow(Curve.rrotate(b, 2))
  694. b = a
  695. a = int_overflow(t << 0)
  696. h0 = int_overflow((h0 + a) << 0)
  697. h1 = int_overflow((h1 + b) << 0)
  698. h2 = int_overflow((h2 + c) << 0)
  699. h3 = int_overflow((h3 + d) << 0)
  700. h4 = int_overflow((h4 + e) << 0)
  701. res = [0] * 20
  702. Curve.packbe(res, 0, h0)
  703. Curve.packbe(res, 4, h1)
  704. Curve.packbe(res, 8, h2)
  705. Curve.packbe(res, 12, h3)
  706. Curve.packbe(res, 16, h4)
  707. return res
  708. @classmethod
  709. def rrotate(cls, v, r):
  710. return unsigned_right_shift(v, r) | (v << (32 - r))
  711. @classmethod
  712. def unpackbe(cls, a, off):
  713. v = 0
  714. for i in range(4):
  715. v |= a[off + i] << (24 - (i * 8))
  716. return v
  717. @classmethod
  718. def packbe(cls, a, off, v):
  719. for i in range(4):
  720. a[off + i] = (v >> (24 - i * 8)) & 0xff
  721. class RC4(object):
  722. def __init__(self):
  723. self.S = []
  724. self.i = 0
  725. self.j = 0
  726. def set_key(self, key):
  727. self.S = [i for i in range(256)]
  728. S, j = self.S, 0
  729. for i in range(256):
  730. j = (j + key[i % len(key)] + S[i]) & 255
  731. S[i], S[j] = S[j], S[i]
  732. for _ in range(768):
  733. self.gen()
  734. def gen(self):
  735. S = self.S
  736. i = self.i = (self.i + 1) & 255
  737. j = self.j = (self.j + S[i]) & 255
  738. S[i], S[j] = S[j], S[i]
  739. return S[(S[i] + S[j]) & 255]
  740. def crypt_uint8array(self, dst, src, start):
  741. for i in range(len(src)):
  742. dst[start + i] = src[i] ^ self.gen()
  743. def encrypt(self, s):
  744. a = ''
  745. for i in range(len(s)):
  746. c = s[i] ^ self.gen()
  747. if c == 0:
  748. c = 256
  749. a += chr(c)
  750. return a
  751. class RouterSession(object):
  752. def __init__(self):
  753. self.id = None
  754. self.pri_key = None
  755. self.pub_key = None
  756. self.padding = [32] * 8
  757. self.rx_seq = 1
  758. self.rx_enc = RC4()
  759. self.tx_seq = 1
  760. self.tx_enc = RC4()
  761. def make_initial_request(self):
  762. self.pri_key = bytes([randint(0, 255) for _ in range(32)])
  763. pub_key = Curve.curve_u2a(
  764. Curve.curve25519(Curve.curve_a2u(self.pri_key)))
  765. self.pub_key = Curve.word2str(
  766. 0) + Curve.word2str(0) + Curve.a2str(pub_key)
  767. self.pub_key = self.pub_key.encode()
  768. def key_exchange(self, body):
  769. self.id = Curve.str2word(body, 0)
  770. r_pub_key = Curve.str2a(body[8:])
  771. master_key = Curve.curve_u2a(Curve.curve25519(
  772. Curve.curve_a2u(self.pri_key), Curve.curve_a2u(r_pub_key)))
  773. self.rx_enc.set_key(self.make_key(master_key, False, False))
  774. self.tx_enc.set_key(self.make_key(master_key, True, False))
  775. def make_key(self, master_key, is_send, is_server):
  776. magic_2 = 'On the client side, this is the send key; on the server side, it is the receive key.'
  777. magic_3 = 'On the client side, this is the receive key; on the server side, it is the send key.'
  778. v = deepcopy(master_key)
  779. v.extend([0 for _ in range(40)])
  780. if is_send == is_server:
  781. v.extend(Curve.str2a(magic_3.encode()))
  782. else:
  783. v.extend(Curve.str2a(magic_2.encode()))
  784. v.extend([0xf2 for _ in range(40)])
  785. return Curve.sha1(v)[:16]
  786. def encrypt_uint8array(self, arr):
  787. narr = [0] * (len(arr) + 16)
  788. narr[1] = self.id >> 16
  789. narr[2] = self.id >> 8
  790. narr[3] = self.id & 0xff
  791. narr[4] = self.tx_seq >> 24
  792. narr[5] = self.tx_seq >> 16
  793. narr[6] = self.tx_seq >> 8
  794. narr[7] = self.tx_seq
  795. self.tx_enc.crypt_uint8array(narr, arr, 8)
  796. for i in range(len(arr) + 8, len(narr)):
  797. narr[i] = 32
  798. xarr = narr[len(arr) + 8:len(arr) + 16]
  799. self.tx_enc.crypt_uint8array(narr, xarr, len(arr) + 8)
  800. self.tx_seq += len(arr) + 8
  801. return bytes(narr)
  802. def decrypt_uint8array(self, arr):
  803. if len(arr) < 16:
  804. return False
  805. _id = int_overflow(arr[0] << 24) | int_overflow(arr[1] << 16) | int_overflow(arr[2] << 8) | arr[3]
  806. seq = int_overflow(arr[4] << 24) | int_overflow(arr[5] << 16) | int_overflow(arr[6] << 8) | arr[7]
  807. if _id != self.id:
  808. return False
  809. if seq != self.rx_seq:
  810. return True
  811. self.rx_seq += len(arr) - 8
  812. self.rx_enc.crypt_uint8array(arr, arr[8:], 8)
  813. for i in range(len(arr) - 8, len(arr)):
  814. if arr[i] != 32:
  815. return False
  816. msgs = Buffer().buffer2msgs(arr[8:len(arr) - 8], 8)
  817. if msgs:
  818. for i in range(len(msgs)):
  819. print(msgs[i])
  820. return True
  821. def encrypt(self, s):
  822. seq = self.tx_seq
  823. self.tx_seq += len(s) + 8
  824. return (Curve.word2str(self.id) + Curve.word2str(seq)) + self.tx_enc.encrypt(s.encode()) + self.tx_enc.encrypt(
  825. self.padding)
  826. def encrypt_uri(self, uri):
  827. s = self.encrypt(uri)
  828. r = ''
  829. for i in range(len(s)):
  830. r += chr(ord(s[i]) & 0xff)
  831. return r
  832. def fetch(self, url, headers, data):
  833. data = self.encrypt_uint8array(Buffer().msg2buffer(data))
  834. response = requests.post(url=url, headers=headers, data=data)
  835. body = list(response.content)
  836. self.decrypt_uint8array(body)
  837. def login(self):
  838. response = requests.post(url=URL, data=self.pub_key)
  839. body = [ord(item) for item in response.content.decode()]
  840. self.key_exchange(body)
  841. data = {'s1': 'root', 's3': ''}
  842. self.fetch(url=URL, headers=HEADERS, data=data)
  843. def change_vpn(self, vpn_idx, vpn_server):
  844. data = {
  845. "U1003c": [9, 0, 0, 0, 0, 0, 0, 0],
  846. "bdd": 0,
  847. "be1": 0,
  848. "be3": 0,
  849. "bfe000a": 0,
  850. "b1000e": 0,
  851. "ufe0001": vpn_idx + 48,
  852. "u10001": 34,
  853. "u10003": 0,
  854. "u10002": 16384,
  855. "uca": 1450,
  856. "ucb": 1450,
  857. "ud9": 4294967294,
  858. "udb": 30,
  859. "udc": 0,
  860. "ude": 60,
  861. "udf": 1,
  862. "sb0004": "disabled",
  863. "s10006": f"Vpn{vpn_idx}",
  864. "s1001e": "l2tp-out",
  865. "s10066": "",
  866. "se0": vpn_server,
  867. "se2": "",
  868. "sfe0009": "",
  869. "sd6": "123qqq",
  870. "sd7": "hnszs3ds",
  871. "Uff0014": [134217944],
  872. "Uff0001": [20, 0],
  873. "uff0007": 16646147
  874. }
  875. self.fetch(url=URL, headers=HEADERS, data=data)
  876. class AdminSession(object):
  877. def __init__(self):
  878. self.username = '17600025055'
  879. self.password = 'zhangyong0712'
  880. self.headers = None
  881. def cookie2str(self, cookies):
  882. ret = []
  883. for key, value in cookies.iteritems():
  884. ret.append(f'{key}={value}')
  885. return '; '.join(ret)
  886. def login(self):
  887. url = 'https://hwq.yycyk.com/'
  888. self.headers = {
  889. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
  890. 'Accept-Encoding': 'gzip, deflate, br',
  891. 'Accept-Language': 'zh-CN,zh;q=0.9',
  892. 'Cache-Control': 'no-cache',
  893. 'Origin': 'https://hwq.yycyk.com',
  894. 'Pragma': 'no-cache',
  895. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
  896. }
  897. response = requests.get(url=url, headers=self.headers)
  898. self.headers.update({'Cookie': self.cookie2str(response.cookies)})
  899. url = 'https://hwq.yycyk.com/passport/loginact'
  900. self.headers.update({
  901. 'Accept': '*/*',
  902. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  903. 'Referer': 'https://hwq.yycyk.com/passport/login',
  904. 'X-Requested-With': 'XMLHttpRequest',
  905. })
  906. data = {
  907. 'phone': self.username,
  908. 'password': self.password,
  909. 'captcha': '',
  910. }
  911. response = requests.post(url=url, headers=self.headers, data=data)
  912. self.headers.update({'Cookie': self.cookie2str(response.cookies)})
  913. def get_proxy_list(self):
  914. url = 'https://hwq.yycyk.com/welcome/dynamicLines'
  915. self.headers.update({'Referer': 'https://hwq.yycyk.com/welcome'})
  916. data = {
  917. 'search_str': '',
  918. 'type': '3',
  919. }
  920. response = requests.post(url=url, headers=self.headers, data=data)
  921. obj = response.json()
  922. proxy_list = []
  923. for item in obj.get('res', {}).get('data', {}).values():
  924. proxy_list.append(item.get('info', {}).get('province_domain'))
  925. for sub_item in item.get('line', []):
  926. if int(sub_item.get('status', 0)) == 0: # 维护中的跳过
  927. continue
  928. if int(sub_item.get('ping', 1000)) >= 200: # 延迟超过200ms的跳过
  929. continue
  930. proxy_list.append(sub_item.get('domain'))
  931. return proxy_list
  932. def job():
  933. admin_session = AdminSession()
  934. admin_session.login()
  935. vpn_server = choice(admin_session.get_proxy_list())
  936. router_session = RouterSession()
  937. router_session.make_initial_request()
  938. router_session.login()
  939. router_session.change_vpn(vpn_idx=1, vpn_server=vpn_server)
  940. now = datetime.now(tz=pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
  941. print(f'[+] {now} 切换代理地址为: {vpn_server}')
  942. def main():
  943. scheduler = BlockingScheduler({
  944. 'apscheduler.timezone': 'Asia/Shanghai',
  945. })
  946. scheduler.add_job(job, 'cron', hour=12, minute=0, second=0)
  947. try:
  948. print('[+] 定时任务已启动')
  949. scheduler.start()
  950. except KeyboardInterrupt:
  951. print('[+] 定时任务已停止')
  952. if __name__ == '__main__':
  953. main()