Skip to content

Instantly share code, notes, and snippets.

@nickytoh
Last active September 6, 2021 03:11
Show Gist options
  • Select an option

  • Save nickytoh/fc0eb01271a45448db16 to your computer and use it in GitHub Desktop.

Select an option

Save nickytoh/fc0eb01271a45448db16 to your computer and use it in GitHub Desktop.

Revisions

  1. Nicky Toh renamed this gist Jun 11, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Nicky Toh created this gist Jun 11, 2014.
    114 changes: 114 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    var uuid = require('node-uuid'),
    https = require('https');

    function iCloud(appleId, password) {

    this.urls = {
    "version" : "https://www.icloud.com/system/version.json",
    "validate": "/setup/ws/1/validate?clientBuildNumber={0}&clientId={1}",
    "login": "/setup/ws/1/login?clientBuildNumber={0}&clientId={1}"
    }

    this.appleId = appleId;
    this.password = password;

    this.clientBuildNumber = '1P24';
    this.clientId = uuid.v1().toString().toUpperCase();

    // console.log('Generated UUID: ' + this.clientId);

    this.cookie = null;
    this.instance = null;

    this.validate();
    }

    iCloud.prototype = {
    validate: function() {
    var me = this;

    var endpoint = this.urls.login
    .replace('{0}', this.clientBuildNumber)
    .replace('{1}', this.clientId);

    // console.log(endpoint);

    var options = {
    host: "p12-setup.icloud.com",
    path: endpoint,
    method: 'POST',
    headers: {
    'Origin': 'https://www.icloud.com',
    'Referer': 'https://www.icloud.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36'
    }
    };

    var data = JSON.stringify({
    apple_id: this.appleId,
    password: this.password,
    extended_login: false
    });

    var request = https.request(options, function(res) {

    if (res.headers['set-cookie']) me.cookie = res.headers['set-cookie'];

    var buffer = '';

    res.on('data', function(data) {
    buffer += data;
    });

    res.on('end', function() {

    me.instance = JSON.parse(buffer);

    var dsid = me.instance.dsInfo.dsid;
    var getContactListUrl = '/co/startup?clientBuildNumber={1}&clientId={2}&clientVersion=2.1&dsid={3}&locale=zh_TW&order=last%2Cfirst'
    .replace('{1}', me.clientBuildNumber)
    .replace('{2}', me.clientId)
    .replace('{3}', dsid); // &id={4}

    var options2 = {
    host: me.instance.webservices.contacts.url.replace('https://', '').replace(':443', ''),
    path: getContactListUrl,
    method: 'GET',
    headers: {
    'Origin': 'https://www.icloud.com',
    'Referer': 'https://www.icloud.com',
    'Cookie': me.cookie.join('; '),
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36'
    }
    };

    var req2 = https.request(options2, function(res) {

    var buf2 = '';
    res.on('data', function(data) {
    buf2 += data;
    });

    res.on('end', function() {
    var contacts = JSON.parse(buf2).contacts;

    for (var i = 0; i < contacts.length; i ++ ) {
    console.log(contacts[i].lastName + ' ' + contacts[i].firstName
    + ', email(' + contacts[i].emailAddresses[0].label + '): ' + contacts[i].emailAddresses[0].field );
    }
    });
    });

    req2.end();

    });
    });

    request.write(data);

    request.end();
    }
    };


    new iCloud('[email protected]', '12345678');