Skip to content

Instantly share code, notes, and snippets.

@RoyalSix
Created July 7, 2017 04:25
Show Gist options
  • Save RoyalSix/036b23be3d0fd8e0693d16136163e9c1 to your computer and use it in GitHub Desktop.
Save RoyalSix/036b23be3d0fd8e0693d16136163e9c1 to your computer and use it in GitHub Desktop.
Online Mode Archive
//OnlineStatus.js
import React from 'react';
import {Glyphicon} from 'react-bootstrap';
// constant declaration
const style = {
textOffline: {
color: "var(--warning-color)",
display: "inline",
backgroundColor: 'var(--background-color-dark)',
outline: 'none',
border: 0
},
textOnline: {
color: "var(--completed-color)",
display: "inline",
backgroundColor: 'var(--background-color-dark)',
outline: 'none',
border: 0
}
};
class OnlineStatus extends React.Component {
constructor() {
super();
this.state = {
online: window.navigator.onLine,
showToggle: false
};
this.setOnline = this.setOnline.bind(this);
this.setOffline = this.setOffline.bind(this);
this.toggleVisibility = this.toggleVisibility.bind(this);
}
componentWillMount() {
window.addEventListener("offline", this.setOffline);
window.addEventListener("online", this.setOnline);
}
componentWillUnmount() {
window.removeEventListener("offline", this.setOffline);
window.removeEventListener("online", this.setOnline);
}
setOnline(fromButton) {
this.props.changeOnlineStatus(true, false, fromButton);
//This designates if the call was actually from a button or not
this.setState({online: true});
}
setOffline(fromButton) {
this.props.changeOnlineStatus(false, false, fromButton);
//This designates if the call was actually from a button or not
this.setState({online: false});
}
toggleVisibility() {
this.setState({showToggle: !this.state.showToggle});
}
render() {
const textStatusColor = this.state.online ? style.textOnline : style.textOffline;
const status = this.state.online ? "Online " : "Offline ";
return (
<div style={textStatusColor} onClick={this.toggleVisibility}>
Status: {status}
<Glyphicon glyph={"triangle-bottom"}
style={{fontSize:10}}
/>
<div onClick={()=>{
this.state.online ? this.setOffline(true) : this.setOnline(true);
//designates a user action for setOffline function
}}
style={{
display: this.state.showToggle ? "block" : "none",
position: "absolute",
zIndex: "9999",
background: "var(--background-color-dark)",
padding: "3px",
borderRadius: "5px",
color: status == "Online " ? "var(--warning-color)" : "var(--completed-color)"
}}>
Switch to {this.state.online ? "Offline" : "Online"}
</div>
</div>
);
}
}
export default OnlineStatus;
//changeOnlineStatusActions.js
import consts from './ActionTypes';
import sudo from 'sudo-prompt';
// constant declarations
const exec = require('child_process').exec;
const options = {
name: 'Translation Core'
};
export function changeOnlineStatus(online, firstLoad, fromButton) {
return ((dispatch) => {
if (!document.hasFocus() && !fromButton) return;
//If the document is out of focus and the action is not created by the user
if (process.platform == 'win32') {
var TCportAllowed = true;
if (firstLoad) {
exec(`netsh advfirewall firewall show rule name="block tc out"`, options, function (error, stdout, stderror) {
if (!error) {
stdout = stdout.replace(/ /g, '');
TCportAllowed = !stdout.includes("Enabled:Yes");
}
dispatch({
type: consts.CHANGE_ONLINE_STATUS,
online: TCportAllowed
});
return;
})
} else {
if (online) {
sudo.exec(`netsh advfirewall firewall delete rule name="block tc in" && netsh advfirewall firewall delete rule name="block tc out"`, options, function (error, stdout, stderror) {
dispatch({
type: consts.CHANGE_ONLINE_STATUS,
online: online
})
})
}
else {
exec(`wmic process where processId=${process.pid} get ExecutablePath`, options, function (error, execPath, stderror) {
execPath = execPath.replace(/\r?\n|\r|\s|ExecutablePath/g, '');
sudo.exec(`netsh advfirewall firewall add rule name="block tc in" dir=in program="${execPath}" action=block && netsh advfirewall firewall add rule name="block tc out" dir=out program="${execPath}" action=block`, options, () => {
dispatch({
type: consts.CHANGE_ONLINE_STATUS,
online: online
});
});
});
}
}
} else {
if (online == window.navigator.onLine && firstLoad) {
dispatch({
type: consts.CHANGE_ONLINE_STATUS,
online: online
});
return;
}
if (online) {
exec('networksetup -setairportpower en1 on', function (cp) {
dispatch({
type: consts.CHANGE_ONLINE_STATUS,
online: online
})
});
} else {
exec('networksetup -setairportpower en1 off', function (cp) {
dispatch({
type: consts.CHANGE_ONLINE_STATUS,
online: online
})
});
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment