Skip to content

Commit

Permalink
Merge pull request #15 from dhfhfk/main
Browse files Browse the repository at this point in the history
Fix KTX login issue
  • Loading branch information
fedebotu authored Mar 7, 2024
2 parents 16fc0ef + f6c46e0 commit 2c18c6a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
36 changes: 34 additions & 2 deletions ktrains/korail/korail.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import itertools
import re
import sys
import base64
from datetime import datetime, timedelta, timezone
from pprint import pprint

import requests
from six import with_metaclass
from Crypto.Util.Padding import pad
from Crypto.Cipher import AES

try:
# noinspection PyPackageRequirements
Expand Down Expand Up @@ -56,6 +59,8 @@ def _python3():
KORAIL_PAYMENT = "%s/ebizmw/PrdPkgMainList.do" % KORAIL_DOMAIN
KORAIL_PAYMENT_VOUCHER = "%s/ebizmw/PrdPkgBoucherView.do" % KORAIL_DOMAIN

KORAIL_CODE = "%s.common.code.do" % KORAIL_MOBILE

DEFAULT_USER_AGENT = "Dalvik/2.1.0 (Linux; U; Android 5.1.1; Nexus 4 Build/LMY48T)"


Expand Down Expand Up @@ -551,6 +556,8 @@ class Korail(object):
_version = "190617001"
_key = "korail1234567890"

_idx = None

membership_number = None
name = None
email = None
Expand All @@ -564,6 +571,30 @@ def __init__(self, korail_id, korail_pw, auto_login=True, want_feedback=False):
if auto_login:
self.login(korail_id, korail_pw)

def __enc_password(self, password):
url = KORAIL_CODE
data = {
'code': "app.login.cphd"
}

r = self._session.post(url, data=data)
j = json.loads(r.text)

if j['strResult'] == 'SUCC' and j.get('app.login.cphd') is not None:
self._idx = j['app.login.cphd']['idx']
key = j['app.login.cphd']['key']

encrypt_key = key.encode(encoding='utf-8', errors='strict')
iv = key[:16].encode(encoding='utf-8', errors='strict')
cipher = AES.new(encrypt_key, AES.MODE_CBC, iv)

padded_data = pad(password.encode("utf-8"), AES.block_size)

return base64.b64encode(base64.b64encode(cipher.encrypt(padded_data))).decode("utf-8")
else:
return False


def login(self, korail_id=None, korail_pw=None):
"""Login to Korail server.
:param korail_id : `Korail membership number` or `phone number` or `email`
Expand Down Expand Up @@ -611,14 +642,15 @@ def login(self, korail_id=None, korail_pw=None):
url = KORAIL_LOGIN
data = {
"Device": self._device,
"Version": "150718001", # HACK
"Version": "231231001", # HACK
#'Version': self._version,
# 2 : for membership number,
# 4 : for phone number,
# 5 : for email,
"txtInputFlg": txt_input_flg,
"txtMemberNo": korail_id,
"txtPwd": korail_pw,
'txtPwd': self.__enc_password(korail_pw),
'idx': self._idx
}

r = self._session.post(url, data=data)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ streamlit-aggrid
pandas
chime
datetime
pytz
pytz
PyCryptodome

0 comments on commit 2c18c6a

Please sign in to comment.