summaryrefslogtreecommitdiff
path: root/iptv.sh
blob: 400bed339ba202b9ec0ac6707bfe0b2c57e6333a (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env bash
set -euo pipefail

# cURL configurations applied to all requests
CURL_ARGS=(--silent --max-time 6)

# Color definitions
COLOR_DANGER=$(tput setaf 1)
COLOR_SUCCESS=$(tput setaf 2)
COLOR_RESET=$(tput sgr0)

show_usage () {
  echo "Usage: $0 [COMMAND] [OPTIONS]"
  echo ""
  echo "Commands:"
  echo "  make:playlist     Make a playlist file"
  echo "  make:epg          Make an EPG file"
  echo "  decrypt:authinfo  Find a possible IPTV key"
  echo ""
  echo "Run '$0 COMMAND -h' for help on a specific command."
}

show_make_playlist_usage () {
  echo "Usage: $0 make:playlist -u <user-id> -p <password> -d <device-id> --mac <mac-address>"
  echo ""
  echo "Options:"
  echo "  -u | --user       User ID"
  echo "  -p | --password   Password"
  echo "  -d | --device     Device ID"
  echo "  --ip              Device IP address (default: 127.0.0.1)"
  echo "  --mac             Device MAC address"
  echo "  --udpxy           Udpxy endpoint (default: http://127.0.0.1:4022)"
  echo "  -o | --output     Playlist file name (default: playlist.M3U8)"
  echo "  -I | --interface  Network interface for cURL requests (optional)"
}

show_make_epg_usage () {
  echo "Usage: $0 make:epg"
  echo ""
  echo "Options:"
  echo "  --endpoint        EPG index endpoint (default: http://120.87.12.38:8083/epg/api/page/biz_59417088.json)"
  echo "  -o | --output     EPG file name (default: epg.xml)"
  echo "  -I | --interface  Network interface for cURL requests (optional)"
}

show_decrypt_authinfo_usage () {
  echo "Usage: $0 decrypt:authinfo <authinfo>"
}

step () { printf '%s==> %s%s\n' "$COLOR_SUCCESS" "$1" "$COLOR_RESET"; }

fatal () { printf '%s!!! %s%s\n' "$COLOR_DANGER" "$1" "$COLOR_RESET" >&2; exit 1; }

ensure_curl_is_installed () {
  if ! command -v curl &>/dev/null; then
    fatal "The cURL package that handles HTTP requests is not installed."
  fi
}

ensure_jq_is_installed () {
  if ! command -v jq &>/dev/null; then
    fatal "The jq package that handles HTTP JSON response is not installed."
  fi
}

ensure_openssl_is_installed () {
  if ! command -v openssl &>/dev/null; then
    fatal "The OpenSSL package that handles token computation is not installed."
  fi
}

categorize_by_channel_name () {
  result="720P"

  case "$1" in
    *4K*) result="4K" ;;
    *"超清"*|*"高清"*) result="1080P" ;;
  esac

  echo "$result"
}

retrieve_endpoint () {
  local user_id="$1"
  local url="http://eds1.unicomgd.com:8082/EDS/jsp/AuthenticationURL?Action=Login&UserID=$user_id&return_type=1"
  local response
  local epgurl
  response=$(curl "${CURL_ARGS[@]}" "$url") || return 1
  epgurl=$(printf '%s' "$response" | jq -r '.epgurl')
  printf '%s\n' "$(extract_hostname "$epgurl")"
}

extract_hostname () {
  local url="$1"
  local res
  res="${url#*//}"
  res="${res%%/*}"
  printf '%s\n' "$res"
}

retrieve_encry_token () {
  local endpoint="$1"
  local user_id="$2"
  local url="http://$endpoint/EPG/oauth/v2/authorize?response_type=EncryToken&client_id=jiulian&userid=$user_id"
  local response
  local token
  response=$(curl "${CURL_ARGS[@]}" "$url") || return 1
  token=$(printf '%s' "$response" | jq -r '.EncryToken') || return 1
  [[ -n "$token" && "$token" != "null" ]] || return 1
  printf '%s\n' "$token"
}

build_authinfo () {
  local encry_token="$1"
  local user_id="$2"
  local device_id="$3"
  local ip_address="$4"
  local mac_address="$5"

  local random_num=$(( (RANDOM << 15 | RANDOM) % 99999999 + 1 ))
  local parts=("$random_num" "$encry_token" "$user_id" "$device_id" "$ip_address" "$mac_address" "Reserved" "OTT")
  local joined
  joined=$(IFS='$'; printf '%s' "${parts[*]}")

  printf '%s\n' "$joined"
}

encrypt_authinfo () {
  local data="$1"
  local key="$2"
  local res
  res=$(printf '%s' "$data" | openssl enc -e -des-ede3 -K "$key" -nosalt 2>/dev/null | xxd -p -u -c 0)

  printf '%s\n' "$res"
}

retrieve_access_token () {
  local endpoint="$1"
  local user_id="$2"
  local authinfo="$3"
  local url="http://$endpoint/EPG/oauth/v2/token?grant_type=EncryToken&client_id=jiulian&UserID=$user_id&DeviceType=UNT400G&DeviceVersion=5.5.021&authinfo=$authinfo&issmarthomestb=1&tvdesktopid="
  local response
  local token
  response=$(curl "${CURL_ARGS[@]}" "$url") || return 1
  token=$(printf '%s' "$response" | jq -r '.access_token') || return 1
  [[ -n "$token" && "$token" != "null" ]] || return 1
  printf '%s\n' "$token"
}

build_channel_logos () {
  local -n ref=$1
  [[ -n "$1" ]] || return 1

  local url='http://120.87.12.38:8083/epg/api/custom/getAllChannel.json'
  local response
  response=$(curl "${CURL_ARGS[@]}" "$url") || return 1
  [[ -n "$response" ]] || return 1

  local channel
  local hwcode
  local icon
  while read -r channel; do
    hwcode=$(printf '%s' "$channel" | jq -r '.params.hwcode') || return 1
    icon=$(printf '%s' "$channel" | jq -r '.icon') || return 1
    [[ -n "$hwcode" && "$hwcode" != "null" ]] || continue
    [[ -n "$icon" && "$icon" != "null" ]] || continue
    ref["$hwcode"]="$icon"
  done < <(printf '%s' "$response" | jq -c '.channels[]') || return 1
}

make_epg () {
  # Check dependencies
  ensure_curl_is_installed
  ensure_jq_is_installed

  # Default variables
  local output_file='epg.xml'
  local endpoint='http://120.87.12.38:8083/epg/api/page/biz_59417088.json'

  while [ $# -gt 0 ]; do
    case "$1" in
      -o | --output ) output_file="$2"; shift 2;;
      --endpoint ) endpoint="$2"; shift 2;;
      -I | --interface ) CURL_ARGS+=(--interface "$2"); shift 2;;
      -h | --help ) show_make_epg_usage; exit 0;;
      *) echo "Unknown option: $1"; exit 1;;
    esac
  done

  step "Request channel index"
  response=$(curl "${CURL_ARGS[@]}" "$endpoint")

  step "Create the EPG XML file"
  echo '<?xml version="1.0" encoding="UTF-8"?>' > "$output_file"
  echo '<!DOCTYPE tv SYSTEM "xmltv.dtd">' >> "$output_file"
  echo "<tv date=\"$(date +"%Y%m%d%H%M%S %z")\">" >> "$output_file"

  while read -r item; do
    item_title=$(echo "$item" | jq -r '.itemTitle')
    item_type=$(echo "$item" | jq -r '.itemType')
    data_link=$(echo "$item" | jq -r '.dataLink')

    if [[ "$item_type" == "channel" ]]; then
      step "Process: $item_title"

      response=$(curl "${CURL_ARGS[@]}" "$data_link")
      channel_icon=$(echo "$response" | jq -r '.channel.icon')
      channel_title=$(echo "$response" | jq -r '.channel.title')
      hwcode=$(echo "$response" | jq -r '.channel.params.hwcode')

      echo "  <channel id=\"$hwcode\">" >> "$output_file"
      echo "    <display-name lang=\"zh\"><![CDATA[$channel_title]]></display-name>" >> "$output_file"
      echo "    <icon src=\"$channel_icon\"/>" >> "$output_file"
      echo "  </channel>" >> "$output_file"

      while read -r schedule; do
        schedule_title=$(echo "$schedule" | jq -r '.title')
        schedule_starttime=$(echo "$schedule" | jq -r '.starttime')
        schedule_endtime=$(echo "$schedule" | jq -r '.endtime')

        echo "  <programme start=\"$schedule_starttime +0800\" stop=\"$schedule_endtime +0800\" channel=\"$hwcode\">" >> "$output_file"
        echo "    <title lang=\"zh\"><![CDATA[$schedule_title]]></title>" >> "$output_file"
        echo "  </programme>" >> "$output_file"
      done < <(echo "$response" | jq -c '.schedules[]')
    fi
  done < <(echo "$response" | jq -c '.areaDatas[].items[]')

  echo '</tv>' >> "$output_file"

  step "EPG built sucessfully!"

  exit 0
}

make_playlist () {
  # Check dependencies
  ensure_curl_is_installed
  ensure_jq_is_installed
  ensure_openssl_is_installed

  # Default values
  local user_id=''
  local password=''
  local device_id=''
  local mac_address=''
  local ip_address='127.0.0.1'
  local udpxy_endpoint='http://127.0.0.1:4022'
  local output_file='playlist.M3U8'

  while [ $# -gt 0 ]; do
    case "$1" in
      -u | --user) user_id="$2"; shift 2;;
      -p | --password) password="$2"; shift 2;;
      -d | --device) device_id="$2"; shift 2;;
      --ip) ip_address="$2"; shift 2;;
      --mac) mac_address="$2"; shift 2;;
      --udpxy) udpxy_endpoint="$2"; shift 2;;
      -o | --output) output_file="$2"; shift 2;;
      -I | --interface) CURL_ARGS+=(--interface "$2"); shift 2;;
      -h | --help ) show_make_playlist_usage; exit 0;;
      *) echo "Unknown option: $1"; exit 1;;
    esac
  done

  # Validate user input
  if [[ -z "$user_id" ]]; then
    fatal "Requires an IPTV user ID with -u or --user"
  fi

  if [[ -z "$password" ]]; then
    fatal "Requires an IPTV password with -p or --password"
  fi

  if [[ -z "$device_id" ]]; then
    fatal "Requires a device ID with -d or --device"
  fi

  if [[ -z "$mac_address" ]]; then
    fatal "Requires a device MAC address with --mac"
  fi

  step "Authenticate"

  local host
  host=$(retrieve_endpoint "$user_id") || fatal 'Failed to retrieve endpoint'

  local encry_token
  encry_token=$(retrieve_encry_token "$host" "$user_id") || fatal 'Failed to retrieve encry token'

  local key
  key=$(printf "%-24s" "$password" | tr ' ' 0 | xxd -p)

  local authinfo
  authinfo=$(build_authinfo "$encry_token" "$user_id" "$device_id" "$ip_address" "$mac_address") || fatal 'Failed to build authinfo'
  authinfo=$(encrypt_authinfo "$authinfo" "$key") || fatal 'Failed to encrypt authinfo'

  local access_token
  access_token=$(retrieve_access_token "$host" "$user_id" "$authinfo") || fatal 'Failed to retrieve access token'

  step "Request channel logos"
  local -A logos
  build_channel_logos logos

  step "Request channels from the batch API"
  response=$(curl "${CURL_ARGS[@]}" \
    --data '{"channelcodes":""}' \
    --header 'Content-Type: application/json;charset=utf-8' \
    --header "Authorization: $access_token" \
    http://$host/EPG/interEpg/channellist/batch)

  step "Decode channels from the HTTP response"
  echo '#EXTM3U' > "$output_file"
  echo '#EXT-X-VERSION:3' >> "$output_file"

  echo "$response" | jq -c '.channellist[]' | while read -r channel; do
    channelcode=$(echo "$channel" | jq -r '.channelcode')
    channelname=$(echo "$channel" | jq -r '.channelname')
    channelurl=$(echo "$channel" | jq -r '.channelurl')
    IFS='|' read -ra channelurls <<< "$channelurl"

    step "Process: $channelname"

    pattern="://([^/]+)"
    if [[ "${channelurls[0]}" =~ $pattern ]]; then
      host=${BASH_REMATCH[1]}

      group=$(categorize_by_channel_name "$channelname")

      printf '#EXTINF:-1 tvg-id="%s" tvg-name="%s" tvg-logo="%s" group-name="%s",%s\n' \
        "$channelcode" \
        "$channelname" \
        "${logos[$channelcode]-}" \
        "$group" \
        "$channelname" >> "$output_file"
      echo "$udpxy_endpoint/udp/$host" >> "$output_file"
    fi
  done

  step "Extract IPTV channels successfully!"

  exit 0
}

decrypt_authinfo () {
  if [[ "$1" == '-h' || "$1" == "--help" ]]; then
    show_decrypt_authinfo_usage
    exit 0
  fi

  local authinfo="$1"

  if [[ -z "$authinfo" ]]; then
    fatal "Requires authinfo data from /EPG/oauth/v2/token"
  fi

  authinfo=$(echo -n "$authinfo" | xxd -r -p)

  local loading=("Still working." "Still working.." "Still working...")

  local key
  for ((i=0; i<1000000; i++)); do
    key=$(printf "%06d" "$i")

    if (( i % 100 == 0 )); then
      echo -ne "\r\033[K${loading[i % 3]} $((i / 1000000 * 100))%"
    fi

    local result=$(echo -n "$authinfo" | \
      openssl enc -d -des-ede3 -K "$(echo -n "${key}000000000000000000" | xxd -p -c 0)" 2>/dev/null | \
      tr -d '\0')

    if echo -n "$result" | grep -q OTT; then
      IFS='$' read -ra components <<< "$result"

      echo -e "\n"
      echo "========================================"
      echo "Found key: $key"
      echo "========================================"
      echo "$result"
      echo "========================================"
      echo "      random:  ${components[0]}"
      echo " encry token:  ${components[1]}"
      echo "     user id:  ${components[2]}"
      echo "   device id:  ${components[3]}"
      echo "  ip address:  ${components[4]}"
      echo " mac address:  ${components[5]}"
      echo "    reserved:  ${components[6]}"
      echo "         ott:  ${components[7]}"

      exit 0
    fi
  done

  fatal "Could not find a possible key."
}

##
## Main
##

while [ $# -gt 0 ]; do
  case "$1" in
    make:playlist) shift 1; make_playlist "$@";;
    make:epg) shift 1; make_epg "$@";;
    decrypt:authinfo) shift 1; decrypt_authinfo "$@";;
    -h | --help) show_usage; exit 0;;
    *) echo "Unknown command: $1"; exit 1;;
  esac
done