summaryrefslogtreecommitdiff
path: root/iptv.sh
blob: 0c304679eb137d608c9353b545cfbbb69a5e0ba9 (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
#!/usr/bin/env bash

user_id=""
password=""
device_id=""
ip_address="127.0.0.1"
mac_address=""
udpxy_endpoint="http://127.0.0.1:4022"
output_file="iptv.M3U8"
curl_args=""
endpoint="http://eds1.unicomgd.com:8082"

echo "[-] Authenticate"

##
## Retreive Authentication URL
##

response=$(curl --silent $curl_args \
  "$endpoint/EDS/jsp/AuthenticationURL?Action=Login&UserID=$user_id&return_type=1")

epgurl=$(echo "$response" | jq -r '.epgurl')
host="${epgurl#*//}"
host="${host%%/*}"

##
## Retreive token
##

response=$(curl --silent $curl_args \
  "http://$host/EPG/oauth/v2/authorize?response_type=EncryToken&client_id=jiulian&userid=$user_id")

encry_token=$(echo "$response" | jq -r '.EncryToken')

##
## Authenticate
##

pass=$(printf "%-024s" "$password" | tr ' ' 0 | xxd -p)

authinfo=(
    $(shuf -i 1-99999999 -n 1) $encry_token
    $user_id $device_id
    $ip_address $mac_address
    "Reserved" "OTT"
)

authinfo=$(IFS='$'; echo "${authinfo[*]}")

authinfo=$(echo -n $authinfo | \
    openssl enc -e -des-ede3 -K $pass -nosalt 2>/dev/null | \
    xxd -p -u -c 0)

response=$(curl --silent $curl_args \
    "http://$host/EPG/oauth/v2/token?grant_type=EncryToken&client_id=jiulian&UserID=$user_id&DeviceType=UNT400G&DeviceVersion=5.5.021&authinfo=$authinfo&issmarthomestb=1&tvdesktopid=")

access_token=$(echo "$response" | jq -r '.access_token')

echo "[-] Request channel metas"

declare -A logos

response=$(curl --silent $curl_args \
  http://120.87.12.38:8083/epg/api/custom/getAllChannel.json)

while read -r channel; do
  hwcode=$(echo "$channel" | jq -r '.params.hwcode')
  icon=$(echo "$channel" | jq -r '.icon')

  logos["$hwcode"]="$icon"
done < <(echo "$response" | jq -c '.channels[]')

echo "[-] Request channels from the batch API"

response=$(curl --silent $curl_args \
    --data '{"channelcodes":""}' \
    --header 'Content-Type: application/json;charset=utf-8' \
    --header "Authorization: $access_token" \
    http://$host/EPG/interEpg/channellist/batch)

echo "[-] 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"

  echo "[-] Process: $channelname"

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

    group="720P"
    case "$channelname" in
      *"4K"*)
        group="4K"
        ;;
      *"超清"*|*"高清"*)
        group="1080P"
        ;;
      \?)
        group="720P"
        ;;
    esac

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

echo "[-] Extract IPTV channels successfully!"