利用Python获取Arukas.io IP地址及端口

Arukas.io,一个小日本的Docker服务,类似国内的时速云,灵雀云等容器服务商,现在处于测试阶段,每个用户都可以免费创建十个容器。可以用做梯子,速度还不错,看油管(Youtube)1080P没什么压力,这么说来都挺好的,但是因为是免费测试阶段,IP地址以及端口每隔几天都会改变,每次使用都要手动获取IP and port 等,着实麻烦,对于一个有着各种强迫症(懒)的人来说,是不能容忍的。便想着弄一个小脚本出来,让他自动更新IP and port。然后就动手做了起来。
6.png

花了一个星期做了出来(( ╯□╰ )拖延症,第一天就写的差不多了!!)

代码

文件需要放在Shadowsocks根目录下,此程序只针对windows Shadowsocks其他系统以及其他版本未经测试
运行环境 python3

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2016/12/12 18:03
# @Author  : Aimuz
# @File    : ss-account-update.py
# @Software: PyCharm Community Edition

import urllib
import urllib.request
import urllib.parse
import http.cookiejar
import re
import json

class ArukaUpdate():
    def __init__(self, users):
        data = {}
        for i in range(len(users)):
            cont, cookie = self.login(users[i]['user'], users[i]['password'])
            cont = self.get('https://app.arukas.io/api/containers', cookie)
            cont = json.loads(cont)
            data = self.parser(cont, data=data)

        print(self.save(data))

    def get(self, url, header):
        if url is None:
            return None
        request = urllib.request.Request(url, headers=header)
        return urllib.request.urlopen(request).read()

    def post(self, url, data, header=None):
        if url is None:
            return None
        data = urllib.parse.urlencode(data).encode('utf-8')
        header = {
            'If-None-Match': 'W/"fd4ef77ca641a702611778a254e0b456"'
        }
        request = urllib.request.Request(url, data, headers=header)
        cookies = http.cookiejar.CookieJar()
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookies))
        read = opener.open(request).read()
        cookie = ""
        for ck in cookies:  # 完整cookies
            cookie += ck.name + "=" + ck.value + ";"
        header.setdefault("Cookie", cookie)
        return read, header

    def login(self, user, password):
        if user is None or password is None:
            return None
        data = {
            "email": "%s" % user,
            "password": "%s" % password
        }
        return self.post("https://app.arukas.io/api/login", data)  # 返回 结果以及 cookie

    def parser(self, cont, data=None):
        if cont is None:
            return None
        list = []
        if data:
            list.extend(data['data'])
        for i in range(len(cont['data'])):
            dd = dict()
            attributes = cont['data'][i]['attributes']['port_mappings'][0][0]
            ip = re.findall(r'\d{1,3}-\d{1,3}-\d{1,3}-\d{1,3}', attributes['host'])[0].replace('-', '.')
            port = attributes['service_port']
            dd['ip'] = ip
            dd['prot'] = port
            list.append(dd)
        data = {
            'data': list
        }
        return data

    def save(self, data):
        list = []
        for i in range(len(data['data'])):
            dd = {
                "auth": False,
                "method": "aes-256-cfb",
                "password": "3623294534",  # Shadowsocks 服务设置的密码,暂时只能统一密码
                "remarks": "Shadowsocks",
                "server": data['data'][i]['ip'],
                "server_port": data['data'][i]['prot'],
                "timeout": 5
            }
            list.append(dd)
        with open('gui-config.json', 'r') as f:
            config = json.load(f)
        config['configs'] = list
        try:
            with open('gui-config.json', 'w') as f:
                json.dump(config, f, indent=4, sort_keys=True)
            return 200
        except:
            return 201

if __name__ == '__main__':
    users = [
        {
            "user": "you Arukas email",
            "password": "you Arukas password"
        }
    ]
    ArukaUpdate(users)