Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

Commit

Permalink
added power state support
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsan-a committed Apr 8, 2021
1 parent c289181 commit fc5634c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
4 changes: 0 additions & 4 deletions Phonelink/Phonelink/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
<add key="checkNewRelease" value="true" />
<add key="passwordEnabled" value="true" />
<add key="currentSavePath" value="savedFiles"/>


<!-- please don't touch anything below this. (please)-->
<add key="currentVersion" value="1.3.0" />
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
Expand Down
53 changes: 44 additions & 9 deletions Phonelink/Phonelink/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows.Forms;

namespace Phonelink
{
public partial class Form1 : Form
{
static string AppVersion = "1.4.0";

[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern int ExitWindowsEx(uint uFlags, uint dwReason);

//needed to lock PC
[System.Runtime.InteropServices.DllImport("user32")]
public static extern void LockWorkStation();

public Form1()
{
InitializeComponent();
Expand All @@ -24,24 +33,24 @@ private void Form1_Load(object sender, EventArgs e)
{
UpdateMenus();
SendUpdateNotif();
Route.Add("/", (req, res, props) =>
Route.Add("/", (req, res, args) =>
{
res.AsText("server is up, send a link!");
});

var baseUrl = Convert.ToBoolean(Config.AppSettings.Settings["passwordEnabled"].Value) ? "/" + Config.AppSettings.Settings["password"].Value + "/" : "/";
Route.Add($"{baseUrl}url/{{url}}", (req, res, props) =>
Route.Add($"{baseUrl}url/{{url}}", (req, res, args) =>
{
var url = props["url"];
var url = args["url"];
if (!(url.StartsWith("http"))) url = $"http://{url}";
System.Diagnostics.Process.Start($"{url}");
res.AsText($"opened {url} on your computer.");
GC.Collect();
});

Route.Add($"{baseUrl}file", (req, res, props) =>
Route.Add($"{baseUrl}file", (req, res, args) =>
{
SaveFile(req, res, props);
SaveFile(req, res, args);
GC.Collect();
},
"POST");
Expand All @@ -51,6 +60,12 @@ private void Form1_Load(object sender, EventArgs e)
SendNotification(req.Headers["title"], req.Headers["body"]);
res.AsText($"sent a notification with the title as \"{req.Headers["title"]}\" and the content body as \"{req.Headers["body"]}\"");
});

Route.Add($"{baseUrl}power/{{state}}", (req, res, args) =>
{
Console.WriteLine("called");
res.AsText(handlePower(args["state"]));
});


HttpServer.ListenAsync(
Expand All @@ -62,10 +77,10 @@ private void Form1_Load(object sender, EventArgs e)
Console.WriteLine(baseUrl);
}

private void SendNotification(string title, string body)
private void SendNotification(string notifTitle, string body)
{
new ToastContentBuilder()
.AddText(title)
.AddText(notifTitle)
.AddText(body)
.Show();
}
Expand All @@ -75,7 +90,7 @@ private void SendUpdateNotif()
if (Convert.ToBoolean(Config.AppSettings.Settings["passwordEnabled"].Value))
{
var update = new GithubUpdateCheck("ahsan-a", "PhoneLink");
var isUpdate = update.IsUpdateAvailable(ConfigurationManager.AppSettings.Get("currentVersion"), VersionChange.Minor);
var isUpdate = update.IsUpdateAvailable(AppVersion, VersionChange.Minor);
if (isUpdate)
{
SendNotification("PhoneLink Update Available",
Expand Down Expand Up @@ -139,6 +154,26 @@ private string getFileName(string fileName, string path, int i)
return $"{path}/{Path.GetFileNameWithoutExtension(fileName)} ({i}){Path.GetExtension(fileName)}";
}

private string handlePower(string state)
{
switch (state)
{
case "shutdown":
Process.Start("shutdown", "/s /t 0");
return "Shut down successfully.";
case "restart":
Process.Start("shutdown", "/r /t 0");
return "Restarted successfully.";
case "logout":
ExitWindowsEx(0, 0);
return "Logged off successfully.";
case "lock":
LockWorkStation();
return "Locked successfully.";
default: return "Invalid argument.";
}
}

// Auto generated methods

private void Form1_Shown(object sender, EventArgs e)
Expand Down
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@


# PhoneLink

Send links to your computer from your phone, or from any device in your local network.
Expand All @@ -8,6 +9,7 @@ Send links to your computer from your phone, or from any device in your local ne
1. Run the Installer. Then, run the `setup.ps1` file to trust your chosen port, and add it to your firewall. Requests **will not work** if you do not do this. (if you manually installed before I created an installer, please delete your PhoneLink folder from wherever you placed it, and delete your startup shortcut.)
2. Edit your settings by clicking on the icon in your tray. Make sure to change your save location, as it saves it to a folder inside of it's current path by default.
3. Download these shortcuts:
- [PhoneLink (Control power state and send notification)](https://www.icloud.com/shortcuts/17aacb75e9704c45bb65b9e8d748f7dd)
- [Open Link on Computer](https://www.icloud.com/shortcuts/19bfb332f6be4ffd8b5ebcbc55d15cfb)
- [Save File on Computer](https://www.icloud.com/shortcuts/8c3aa77aecb944a9aa4ee5e202ee4bed)

Expand All @@ -25,16 +27,17 @@ Requests look like this: `http://IP:PORT/PASSWORD/TYPE`
and like this without a password: `http://IP:PORT/TYPE`

### Requests:
| Request Type| Request URL Example |Description|
|--------------------|-------------|-----------|
|URL |`192.168.0.2:1234/1234/url/google.com`| A GET request to send a link to your computer. It doesn't need to begin with `http://` or `https://`.|
|File| `192.168.0.2:1234/1234/file` | A POST request to save files on your computer. Your file(s) inside of a form-data body. The key can be anything and the value must be your file(s). You can have multiple of these. The save location is whatever specified inside of your settings.|
| Notification |`192.168.0.2:1234/1234/notification`|A GET request to send a notification to your computer. Your content must be in the form of headers. Your headers are `title`, and `body`. |
|Request Type | Request Format | Request Example | Request Description |
|--|--|--|--|
| URL | `[IP]:[PORT]/[PASSWORD]/url/[URL]` | `192.168.0.2:1234/1234/url/google.co.uk` | A GET request to send a link to your computer. It doesn't need to begin with `http://` or `https://`.|
| File |`[IP]:[PORT]/[PASSWORD]/file`|`192.168.0.2:1234/1234/file`| POST request to save files on your computer. Your file(s) inside of a form-data body. The key can be anything and the value must be your file(s). You can have multiple of these. The save location is whatever specified inside of your settings.|
|Notification|`[IP]:[PORT]/[PASSWORD]/notification`| `192.168.0.2:1234/1234/notification` |A GET request to send a notification to your computer. Your content must be in the form of headers. Your headers are `title`, and `body`. |
|Power|`[IP]:[PORT]/[PASSWORD]/power/[TYPE]`|`192.168.0.2:1234/1234/power/shutdown`|A GET request to control your computer's power state. your `[TYPE]` must be either: `shutdown`, `restart`, `logout`, or `lock`.|

The password is optional.

If you run into any problems, contact me on discord (ahsan#4403), or make an issue.
If you run into any problems make an issue, orcontact me on discord (ahsan#4403).

## Configuration

A settings menu is now available by clicking on the PhoneLink icon in the system tray.
A settings menu is now available by clicking on the PhoneLink icon in the system tray. (v1.2.0)
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

### 1.4.0 (8/4/2021)

- Added the ability to control your power state.
- Changed version from a configuration tag to a variable, meaning your configuration won't have to be cleared every update.

### 1.3.0 (28/3/2021)

- Added the ability to send notifications

### 1.2.0 (27/3/2021)

- Added settings UI.
Expand Down

0 comments on commit fc5634c

Please sign in to comment.