Initial functional

Signed-off-by: Vasiliy Doylov <nekocwd@mainlining.org>
This commit is contained in:
Vasiliy Doylov 2025-06-22 00:55:38 +03:00
parent 95e1c233e7
commit 22cd6fb733
Signed by: NekoCWD
GPG key ID: B7BE22D44474A582
15 changed files with 256 additions and 26 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/subprojects/blueprint-compiler
Sub*

View file

@ -1,3 +1,6 @@
# singularity
# Singularity
A description of this project.
SingBox wrapper.
## ENV variables:
- `SUB_FILE` = `.config/Singularity/subscription` - path to subscription file

View file

@ -41,6 +41,11 @@ public class Singularity.Application : Adw.Application {
base.activate ();
var win = this.active_window ?? new Singularity.Window (this);
win.present ();
var styling = new Gtk.CssProvider ();
styling.load_from_resource ("/io/gitlab/nekocwd/singularity/gtk/style.css");
Gtk.StyleContext.add_provider_for_display (Gdk.Display.get_default (),
styling,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
}
private void on_about_action () {

31
src/base.json Normal file
View file

@ -0,0 +1,31 @@
{
"endpoints": [],
"experimental": {
"clash_api": {
"default_mode": ""
}
},
"inbounds": [
{
"domain_strategy": "",
"listen": "127.0.0.1",
"listen_port": 2080,
"tag": "mixed-in",
"type": "mixed"
}
],
"log": {
"level": "info"
},
"route": {
"final": "proxy",
"find_process": true,
"rule_set": [],
"rules": [
{
"action": "sniff",
"inbound": ["mixed-in"]
}
]
}
}

16
src/gtk/outbound-row.blp Normal file
View file

@ -0,0 +1,16 @@
using Gtk 4.0;
using Adw 1;
template $SingularityUiOutboundRow: Gtk.Box {
styles [
"outbound-row",
]
Label schema {
label: "vless";
}
Label descr {
label: "Placeholder name";
}
}

View file

@ -1,4 +1,4 @@
/* window.vala
/* outbound-row.vala
*
* Copyright 2025 Vasiliy Doylov (NekoCWD) <nekocwd@mainlining.org>
*
@ -18,12 +18,16 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
[GtkTemplate (ui = "/io/gitlab/nekocwd/singularity/window.ui")]
public class Singularity.Window : Adw.ApplicationWindow {
[GtkTemplate(ui = "/io/gitlab/nekocwd/singularity/gtk/outbound-row.ui")]
class Singularity.Ui.OutboundRow : Gtk.Box {
[GtkChild]
private unowned Gtk.Label label;
public Window (Gtk.Application app) {
Object (application: app);
private unowned Gtk.Label schema;
[GtkChild]
private unowned Gtk.Label descr;
construct {
}
public void set_outbound(Outbound.Outbound outbound) {
schema.label = outbound.type_name;
descr.label = outbound.name;
}
}

4
src/gtk/style.css Normal file
View file

@ -0,0 +1,4 @@
.outbound-row {
border-radius: 5px;
padding: 10px;
}

View file

@ -18,12 +18,17 @@ template $SingularityWindow: Adw.ApplicationWindow {
}
}
content: Label label {
label: _("Hello, World!");
content: Adw.Clamp {
ScrolledWindow {
ListView outbounds {
halign: fill;
valign: start;
styles [
"title-1",
]
styles [
"card",
]
}
}
};
};
}

42
src/gtk/window.vala Normal file
View file

@ -0,0 +1,42 @@
/* window.vala
*
* Copyright 2025 Vasiliy Doylov (NekoCWD) <nekocwd@mainlining.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
[GtkTemplate (ui = "/io/gitlab/nekocwd/singularity/gtk/window.ui")]
public class Singularity.Window : Adw.ApplicationWindow {
[GtkChild]
private unowned Gtk.ListView outbounds;
public Window (Gtk.Application app) {
Object (application: app);
var factory = new Gtk.SignalListItemFactory ();
factory.setup.connect ((obj) => {
var item = (Gtk.ListItem) obj;
item.set_child (new Ui.OutboundRow ());
});
factory.bind.connect ((obj) => {
var item = (Gtk.ListItem) obj;
var row = (Ui.OutboundRow) item.child;
var data = (Outbound.Outbound) item.item;
row.set_outbound (data);
});
outbounds.set_model (SingBox.instance.outbound_selection);
outbounds.set_factory (factory);
}
}

View file

@ -15,4 +15,5 @@ singularity_sources += [
config_sources,
'logic/tls.vala',
'logic/error.vala',
'logic/singbox.vala',
]

View file

@ -22,6 +22,7 @@ class Singularity.Outbound.Outbound : Object, Json.Serializable {
// We can't use name `type` in vala
public string type_name { get; construct set; default = ""; }
public string tag { get; set; default = "proxy"; }
public string name; // Not a property
public static Outbound parse_uri (string profile) throws UriError, ParseError {
Uri uri = null;
@ -130,7 +131,7 @@ class Singularity.Outbound.Outbound : Object, Json.Serializable {
warning ("Unknown scheme %s", scheme);
break;
}
outbound.name = name;
return outbound;
}
}

88
src/logic/singbox.vala Normal file
View file

@ -0,0 +1,88 @@
/* singbox.vala
*
* Copyright 2025 Vasiliy Doylov (NekoCWD) <nekocwd@mainlining.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
class Singularity.SingBox : Object {
public ListStore outbound_store = new ListStore (typeof (Outbound.Outbound));
public Gtk.SingleSelection outbound_selection = null;
public Subprocess? singbox = null;
construct {
outbound_selection = new Gtk.SingleSelection (outbound_store);
outbound_selection.notify["selected"].connect (() => {
message ("Selected %s", ((Outbound.Outbound) outbound_selection.selected_item).name);
set_up ();
});
}
private void set_up () {
if (singbox != null) {
message ("killing singbox");
singbox.force_exit ();
}
var conf_dir = File.new_build_filename (Environment.get_user_config_dir (), "Singularity");
try {
conf_dir.make_directory ();
} catch (IOError.EXISTS err) {
// Skip this err
} catch (Error err) {
warning ("Error during creating config dir: %s", err.message);
}
var base_config = conf_dir.get_child ("BaseConfig.json");
if (!base_config.query_exists ()) {
try {
message ("Saving base config");
base_config.replace_contents (resources_lookup_data ("/io/gitlab/nekocwd/singularity/base.json", ResourceLookupFlags.NONE).get_data (), null, false, FileCreateFlags.NONE, null);
} catch (Error err) {
warning ("Error during saving base config: %s", err.message);
}
}
var outbound_config = conf_dir.get_child ("Outbound.json");
try {
message ("Saving outbound config");
var generator = new Json.Generator ();
var config = new SingConfig ();
config.outbounds.append ((Outbound.Outbound) outbound_selection.selected_item);
generator.root = Json.gobject_serialize (config);
generator.set_pretty (true);
outbound_config.replace_contents (generator.to_data (null).data, null, false, FileCreateFlags.NONE, null);
} catch (Error err) {
warning ("Error during saving outbound config: %s", err.message);
}
singbox = new Subprocess.newv ({ "sing-box", "--disable-color", "-c", base_config.get_path (), "-c", outbound_config.get_path (), "run" }, GLib.SubprocessFlags.STDERR_PIPE);
meow_with_stream.begin (new DataInputStream (singbox.get_stderr_pipe ()));
}
async void meow_with_stream (DataInputStream input) {
for (string line = ""; line != null; line = yield input.read_line_async ())
message ("SingBox: %s", line);
message ("SingBox stream closed");
}
private SingBox () {}
private static SingBox _instance = null;
public static SingBox instance {
get {
if (_instance == null)
_instance = new SingBox ();
return _instance;
}
}
}

View file

@ -18,11 +18,35 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
int main (string[] args) {
Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
Intl.textdomain (Config.GETTEXT_PACKAGE);
var app = new Singularity.Application ();
return app.run (args);
namespace Singularity {
int main (string[] args) {
Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
Intl.textdomain (Config.GETTEXT_PACKAGE);
uint8[] content = null;
try {
var sub_path = Environment.get_variable ("SUB_FILE");
File file = null;
if (sub_path != null) {
file = File.new_for_path (sub_path);
} else {
file = File.new_build_filename (Environment.get_user_config_dir (), "Singularity", "subscription");
}
file.load_contents (null, out content, null);
} catch (Error err) {
error ("Failed to parse subscription %s", err.message);
}
var lines = ((string) content).split ("\n", -1);
foreach (var line in lines) {
if (line[0] == '#' && line != "")
continue;
try {
SingBox.instance.outbound_store.append (Outbound.Outbound.parse_uri (line));
} catch (Error err) {
warning ("Error during subscription parse: %s", err.message);
}
}
var app = new Singularity.Application ();
return app.run (args);
}
}

View file

@ -1,7 +1,8 @@
singularity_sources = [
'main.vala',
'application.vala',
'window.vala',
'gtk/window.vala',
'gtk/outbound-row.vala',
]
singularity_deps = [
@ -16,7 +17,8 @@ blueprints = custom_target(
'blueprints',
input: files(
'gtk/help-overlay.blp',
'window.blp',
'gtk/outbound-row.blp',
'gtk/window.blp',
),
output: '.',
command: [

View file

@ -1,7 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<gresources>
<gresource prefix="/io/gitlab/nekocwd/singularity">
<file preprocess="xml-stripblanks">window.ui</file>
<file preprocess="xml-stripblanks">gtk/window.ui</file>
<file preprocess="xml-stripblanks">gtk/help-overlay.ui</file>
<file preprocess="xml-stripblanks">gtk/outbound-row.ui</file>
<file>base.json</file>
<file>gtk/style.css</file>
</gresource>
</gresources>