Skip to content

Commit

Permalink
feat: https
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveVanOpstal committed Dec 22, 2016
1 parent 5d8dbca commit 4b82ef2
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 33 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ __pycache__
npm-debug.log
npm-debug.log.*

.api.key
.settings.json
.pyc

/secure
.api.key
*.pem
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@
"webpack:client:test": "npm run webpack -- --config config/build/webpack.client.test.js",
"prewebpack:server:test": "npm run clean:build:spec:server && npm run lint:server:test",
"webpack:server:test": "npm run webpack -- --config config/build/webpack.server.test.js",
"start": "npm run http-server",
"server": "npm run http-server",
"http-server": "npm run http-server:dev",
"http-server:dev": "npm run webpack-dev-server -- --env.dev --config config/build/webpack.client.js --watch --content-base src/",
"http-server:prod": "node dist/server/http-server.js",
"start": "npm run http-server:dev",
"server": "npm run http-server:dev",
"http-server:dev": "npm run http-server -- --env.dev",
"http-server:prod": "npm run http-server",
"http-server": "npm run webpack-dev-server -- --config config/build/webpack.client.js --watch --content-base src/",
"http-server-secure:dev": "npm run http-server-secure -- --env.dev",
"http-server-secure:prod": "npm run http-server-secure",
"http-server-secure": "npm run http-server -- --https --cert=./secure/cert.pem --key=./secure/key.pem",
"static-server": "npm run static-server:dev",
"static-server:dev": "npm run build:server:dev && node build/dist/server/static-server.js",
"static-server:prod": "npm run build:server:prod && node build/dist/server/static-server.js",
Expand Down Expand Up @@ -87,7 +90,7 @@
"rimraf": "./node_modules/.bin/rimraf",
"coveralls": "./node_modules/.bin/coveralls",
"webpack": "./node_modules/.bin/webpack --progress",
"webpack-dev-server": "./node_modules/.bin/webpack-dev-server --inline --progress --hot",
"webpack-dev-server": "./node_modules/.bin/webpack-dev-server --inline --progress --compress",
"clang-format": "./node_modules/.bin/clang-format",
"tslint": "./node_modules/.bin/tslint --config ./config/tslint.json",
"webdriver-manager": "./node_modules/.bin/webdriver-manager",
Expand Down
2 changes: 1 addition & 1 deletion src/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ <h1>Legend Builder</h1>

<% if (htmlWebpackPlugin.options.ENV === 'development') { %>
<!-- Webpack Dev Server reload -->
<script src="http://<%= htmlWebpackPlugin.options.host %>:<%= htmlWebpackPlugin.options.port %>/webpack-dev-server.js"></script>
<script src="https://<%= htmlWebpackPlugin.options.host %>:<%= htmlWebpackPlugin.options.port %>/webpack-dev-server.js"></script>
<% } %>
</body>

Expand Down
5 changes: 3 additions & 2 deletions src/client/services/lolapi.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,16 @@ describe('LolApiService', () => {
inject([LolApiService], (service) => {
expect(service.getEndpoint(Endpoint.static, 'region'))
.toBe(
'http://' + settings.staticServer.host + ':' + settings.staticServer.port +
'https://' + settings.staticServer.host + ':' + settings.staticServer.port +
'/static-data/region/v1.2');
}));

it('should get the correct resolved link to the match-server',
inject([LolApiService], (service) => {
expect(service.getEndpoint(Endpoint.match, 'region'))
.toBe(
'http://' + settings.matchServer.host + ':' + settings.matchServer.port + '/region');
'https://' + settings.matchServer.host + ':' + settings.matchServer.port +
'/region');
}));

it('should not get incorrect params', inject([LolApiService], (service) => {
Expand Down
14 changes: 10 additions & 4 deletions src/client/services/lolapi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ export class LolApiService {
constructor(private http: Http, private router: Router) {}

public getRegions(): Observable<any> {
return this.cache('http://status.leagueoflegends.com/shards').map(res => {
return this.cache('https://status.leagueoflegends.com/shards').map(res => {
res.push({name: 'Public Beta Environment', slug: 'pbe'});
return res;
});
}

public getRealm(): Observable<any> {
return this.get(Endpoint.static, '/realm');
return this.get(Endpoint.static, '/realm').map(res => {
// both http and https are supported, realm data will return http
if (res.cdn) {
res.cdn = res.cdn.replace('http://', 'https://');
}
return res;
});
}

public getChampions(): Observable<any> {
Expand Down Expand Up @@ -90,10 +96,10 @@ export class LolApiService {
private getEndpoint(endpoint: Endpoint, region: string): string {
switch (endpoint) {
case Endpoint.static:
return 'http://' + settings.staticServer.host + ':' + settings.staticServer.port +
return 'https://' + settings.staticServer.host + ':' + settings.staticServer.port +
'/static-data/' + region + '/v1.2';
default:
return 'http://' + settings.matchServer.host + ':' + settings.matchServer.port + '/' +
return 'https://' + settings.matchServer.host + ':' + settings.matchServer.port + '/' +
region;
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/client/shared/ddragon.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TestComponent {

let realm = {
'v': '[realm-version]',
'cdn': 'http://url/cdn',
'cdn': 'https://url/cdn',
'n': {
'champion': '[champion-version]',
'profileicon': '[profileicon-version]',
Expand Down Expand Up @@ -69,32 +69,32 @@ describe('DDragonDirective', () => {

it('should create a correct url', inject([DDragonDirective], (directive) => {
let result = directive.buildImage('test.png', realm);
expect(result).toBe('http://url/cdn/[realm-version]/img/test.png');
expect(result).toBe('https://url/cdn/[realm-version]/img/test.png');
}));

it('should create a correct \'ui\' url', inject([DDragonDirective], (directive) => {
let result = directive.buildImage('ui/test.png', realm);
expect(result).toBe('http://url/cdn/5.5.1/img/ui/test.png');
expect(result).toBe('https://url/cdn/5.5.1/img/ui/test.png');
}));

it('should create a correct \'champion\' url', inject([DDragonDirective], (directive) => {
let result = directive.buildImage('champion/test.png', realm);
expect(result).toBe('http://url/cdn/[champion-version]/img/champion/test.png');
expect(result).toBe('https://url/cdn/[champion-version]/img/champion/test.png');
}));

it('should create a correct \'profileicon\' url', inject([DDragonDirective], (directive) => {
let result = directive.buildImage('profileicon/test.png', realm);
expect(result).toBe('http://url/cdn/[profileicon-version]/img/profileicon/test.png');
expect(result).toBe('https://url/cdn/[profileicon-version]/img/profileicon/test.png');
}));

it('should create a correct \'junk\' url', inject([DDragonDirective], (directive) => {
let result = directive.buildImage('junk/test.png', realm);
expect(result).toBe('http://url/cdn/[realm-version]/img/junk/test.png');
expect(result).toBe('https://url/cdn/[realm-version]/img/junk/test.png');
}));

it('should create a correct \'champion/loading\' url', inject([DDragonDirective], (directive) => {
let result = directive.buildImage('champion/loading/test.png', realm);
expect(result).toBe('http://url/cdn/img/champion/loading/test.png');
expect(result).toBe('https://url/cdn/img/champion/loading/test.png');
}));

it('should not update image without realm', inject([DDragonDirective], (directive) => {
Expand Down Expand Up @@ -129,7 +129,7 @@ describe('DDragonDirective', () => {
directive.ngOnInit();
backend.success(realm);
expect(directive.el.nativeElement.style.background)
.toContain('http://url/cdn/[realm-version]/img/test.png');
.toContain('https://url/cdn/[realm-version]/img/test.png');
expect(directive.el.nativeElement.style.background).toContain('1px 1px');
})));
});
Expand All @@ -148,7 +148,8 @@ describe('DDragonDirective', () => {
it('should set requested image', async(inject([MockBackend], (backend) => {
directive.ngOnInit();
backend.success(realm);
expect(directive.el.nativeElement.src).toBe('http://url/cdn/[realm-version]/img/test.png');
expect(directive.el.nativeElement.src)
.toBe('https://url/cdn/[realm-version]/img/test.png');
})));
});

Expand All @@ -167,7 +168,7 @@ describe('DDragonDirective', () => {
directive.ngOnInit();
backend.success(realm);
expect(directive.el.nativeElement.getAttribute('href'))
.toBe('http://url/cdn/[realm-version]/img/test.png');
.toBe('https://url/cdn/[realm-version]/img/test.png');
})));
});
});
2 changes: 1 addition & 1 deletion src/client/testing/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class MockMockBackend extends MockBackend {
super();
this.connections.subscribe(c => {
this.connection = c;
if (this.connection.request.url === 'http://status.leagueoflegends.com/shards') {
if (this.connection.request.url === 'https://status.leagueoflegends.com/shards') {
this.connection.mockRespond(
new Response(new ResponseOptions({status: 200, body: [{'slug': 'euw'}]})));
}
Expand Down
21 changes: 13 additions & 8 deletions src/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as http from 'http';
import * as fs from 'fs';
import {IncomingMessage, ServerResponse} from 'http';
import * as https from 'https';
import * as url from 'url';
Expand All @@ -11,10 +11,15 @@ import {settings} from '../../config/settings';

let apiKey = '';
try {
apiKey = require('raw-loader!../../.api.key').replace(/^\s+|\s+$/g, '');
apiKey = fs.readFileSync('./secure/.api.key').toString().replace(/^\s+|\s+$/g, '');
} catch (e) {
}

const ssl = {
key: fs.readFileSync('./secure/key.pem'),
cert: fs.readFileSync('./secure/cert.pem')
};

export interface HostResponse {
data: string;
json?: any;
Expand All @@ -41,7 +46,7 @@ export class Server {

public headers = {
'Access-Control-Allow-Origin':
'http://' + settings.httpServer.host + ':' + settings.httpServer.port,
'https://' + settings.httpServer.host + ':' + settings.httpServer.port,
'content-type': 'application/json'
};

Expand All @@ -68,12 +73,12 @@ export class Server {
maxAge: 1000 * 60 * 60 * 2
}));

this.merge({Origin: 'http://' + this.host + ':' + this.port}, this.options.headers);
this.merge({Origin: 'https://' + this.host + ':' + this.port}, this.options.headers);
}

public run(callback: (req: IncomingMessage, resp: ServerResponse) => void): void {
this.preRun();
let server = http.createServer((request: IncomingMessage, response: ServerResponse) => {
let server = https.createServer(ssl, (request: IncomingMessage, response: ServerResponse) => {
this.handleRequest(request, response, callback);
});
server.listen(this.port, this.host);
Expand Down Expand Up @@ -137,7 +142,7 @@ export class Server {
private sendHttpRequest(
options: https.RequestOptions, callback: (response: HostResponse) => void) {
let console = new ColorConsole();
let req = http.request(
let req = https.request(
options, (res: IncomingMessage) => this.handleResponse(console, options, res, callback));
req.on(
'error', (e) => this.handleResponseError(
Expand Down Expand Up @@ -227,14 +232,14 @@ export class Server {

private getRegions(callback: (regions: Array<string>) => void) {
let options = {
path: 'http://status.leagueoflegends.com/shards',
path: 'https://status.leagueoflegends.com/shards',
hostname: 'status.leagueoflegends.com',
method: 'GET',
headers: {
'User-Agent': 'Legend-Builder',
'Accept-Language': 'en-US',
'Accept-Charset': 'ISO-8859-1,utf-8',
Origin: 'http://' + this.host + ':' + this.port
Origin: 'https://' + this.host + ':' + this.port
}
};

Expand Down

0 comments on commit 4b82ef2

Please sign in to comment.