diff --git a/manifest.json b/manifest.json index 0d8003e..a25aacc 100644 --- a/manifest.json +++ b/manifest.json @@ -34,7 +34,8 @@ "clipboardRead", "notifications", "bookmarks", - "browserSettings" + "browserSettings", + "proxy" ], "web_accessible_resources": [ "build/console.html", diff --git a/src/background/controllers/CommandController.ts b/src/background/controllers/CommandController.ts index 11fed01..d3697b1 100644 --- a/src/background/controllers/CommandController.ts +++ b/src/background/controllers/CommandController.ts @@ -47,6 +47,8 @@ export default class CommandController { return this.completionsUseCase.queryBdeleteForce(name, keywords); case 'set': return this.completionsUseCase.querySet(name, keywords); + case 'proxy': + return this.completionsUseCase.queryProxy(name, keywords); } return Promise.resolve([]); } @@ -99,6 +101,8 @@ export default class CommandController { case 'h': case 'help': return this.commandIndicator.help(); + case 'proxy': + return this.commandIndicator.proxy(keywords); } throw new Error(words[0] + ' command is not defined'); } diff --git a/src/background/domains/CommandDocs.ts b/src/background/domains/CommandDocs.ts index e926851..8a1ad44 100644 --- a/src/background/domains/CommandDocs.ts +++ b/src/background/domains/CommandDocs.ts @@ -9,4 +9,5 @@ export default { quit: 'Close the current tab', quitall: 'Close all tabs', help: 'Open Vim Vixen help in new tab', + proxy: 'Set a proxy' } as {[key: string]: string}; diff --git a/src/background/repositories/BrowserSettingRepository.ts b/src/background/repositories/BrowserSettingRepository.ts index 9cfb35e..739cd9c 100644 --- a/src/background/repositories/BrowserSettingRepository.ts +++ b/src/background/repositories/BrowserSettingRepository.ts @@ -17,6 +17,17 @@ declare namespace browser.browserSettings.homepageOverride { function get(param: object): Promise; } +declare namespace browser.proxy.settings { + + type BrowserSettings = { + value: string; + }; + + function get(param: object): Promise; + function set(param: object): Promise; + function clear(param: object): Promise; +} + @injectable() export default class BrowserSettingRepository { async getHomepageUrls(): Promise { @@ -24,3 +35,25 @@ export default class BrowserSettingRepository { return value.split('|').map(urls.normalizeUrl); } } + +export class ProxyRepository { + private clearProxySettings(): Promise{ + return browser.proxy.settings.clear({}); + } + + private setProxySettings(address: string): Promise{ + return browser.proxy.settings.set({value: { + proxyType: "manual", + http: address, + httpProxyAll: true + }}); + } + + set(address: string): Promise { + if( address.toLowerCase() == 'none' ){ + return this.clearProxySettings(); + } + return this.setProxySettings(address); + } +} + diff --git a/src/background/usecases/CommandUseCase.ts b/src/background/usecases/CommandUseCase.ts index d757215..70a953f 100644 --- a/src/background/usecases/CommandUseCase.ts +++ b/src/background/usecases/CommandUseCase.ts @@ -10,6 +10,7 @@ import BookmarkRepository from '../repositories/BookmarkRepository'; import ConsoleClient from '../infrastructures/ConsoleClient'; import ContentMessageClient from '../infrastructures/ContentMessageClient'; import RepeatUseCase from '../usecases/RepeatUseCase'; +import { ProxyRepository } from '../repositories/BrowserSettingRepository'; @injectable() export default class CommandIndicator { @@ -22,6 +23,7 @@ export default class CommandIndicator { private consoleClient: ConsoleClient, private contentMessageClient: ContentMessageClient, private repeatUseCase: RepeatUseCase, + private proxyRepository: ProxyRepository, ) { } @@ -142,6 +144,13 @@ export default class CommandIndicator { return this.helpPresenter.open(); } + async proxy(keywords: string): Promise { + if (keywords.length === 0) { + return this.proxyRepository.set('none'); + } + return this.proxyRepository.set(keywords); + } + private async urlOrSearch(keywords: string): Promise { let settings = await this.settingRepository.get(); return urls.searchUrl(keywords, settings.search); diff --git a/src/background/usecases/CompletionsUseCase.ts b/src/background/usecases/CompletionsUseCase.ts index bfff1e6..e81c41c 100644 --- a/src/background/usecases/CompletionsUseCase.ts +++ b/src/background/usecases/CompletionsUseCase.ts @@ -217,4 +217,36 @@ export default class CompletionsUseCase { url: page.url })); } + + async queryProxyItems(name: string, keywords: string) { + let proxies = [{ + caption: "burp", + url: "127.0.0.1:8080" + },{ + caption: "none", + url: "none" + } ].filter(proxy=>{ + if( keywords == '' ){ return true; } + if( proxy.caption.startsWith(keywords) ){ return true; } + if( proxy.url.startsWith(keywords) ){ return true; } + return false; + }); + return proxies; + return proxies.map(proxy => ({ + caption: proxy.caption, + content: name + ' ' + proxy.url, + url: proxy.url + })); + } + + async queryProxy(name: string, keywords: string): Promise { + let groups: CompletionGroup[] = []; + let items = await this.queryProxyItems( name, keywords ); + groups.push({ + name: 'Saved Proxies', + items: items + }); + + return groups; + } } diff --git a/src/shared/settings/Settings.ts b/src/shared/settings/Settings.ts index 97dda7f..2c01551 100644 --- a/src/shared/settings/Settings.ts +++ b/src/shared/settings/Settings.ts @@ -9,6 +9,7 @@ import validate from './validate'; export type SettingsJSON = { keymaps?: KeymapsJSON, search?: SearchJSON, + //proxies?: ProxiesJSON, properties?: PropertiesJSON, blacklist?: BlacklistJSON, }; @@ -162,5 +163,10 @@ export const DefaultSettingJSONText = `{ ] }`; + //"proxies": { + // "burp": "127.0.0.1:8080", + // "none": "none" + //}, + export const DefaultSetting: Settings = Settings.fromJSON(JSON.parse(DefaultSettingJSONText));