Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpchen committed Dec 25, 2024
1 parent 228c4d8 commit 4cec095
Show file tree
Hide file tree
Showing 20 changed files with 297 additions and 71 deletions.
74 changes: 74 additions & 0 deletions docs/document/Articles/docs/SSH Quick Start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# OpenSSH Quick Start

## The Authentication

A host machine that running a SSH server can be called as **Remote** which should store *public keys* for client hosts.
A host machine(client) attempting to connect with Remote should know a *private key* and *passphrase*.
This is how the authentication happen:

1. Client sends request to Remote.
2. If the Client had never connect to the Remote, warning prompts to confirm connection implying it may be dangerous.
3. If any public key on Remote matches the client, Remote requests the client for authenticator which should be calculated by *passphrase* and *private key*
4. User enter the *passphrase* to decrypt the private key, SSH auto-calculates the authenticator, then send it to the Remote.

## Client Side

- Generate public key for connecting **one or more** remotes by `ssh-keygen`
```sh
ssh-keygen -t rsa
```
- Register new public key to remote by `ssh-copy-id`(may require login on remote)
```sh
# add public key under `~/.ssh/` to `~/.ssh/authorized_keys` on <remote>
ssh-copy-id -i <pub_key_file> [<username>@]<remote>
```
- Managing authenticated state and **agent forwarding** by `ssh-agent`
```sh
ssh-agent $SHELL
```
- Add private key to `ssh-agent` to memorize the state of the key by `ssh-add`
```sh
ssh-add ~/.ssh/id_ed25519
ssh-add -l # list all private keys memorized by ssh-agent
ssh-add -d id_ed25519 # inform ssh-agent to forget this key
ssh-add -D # forget all keys
```
- Managing once connected remote in `~/.ssh/known_hosts`
- File transfer by `scp`
- Modify *passphrase* by `ssh-keygen` if you forgot
```sh
ssh-keygen -p
```

## Server Side

- Start SSH server by `sshd`
- Manage SSH server config by `/etc/ssh/sshd_config`
- Manage public key for multiple clients in `~/.ssh/authorized_keys`
> [!NOTE]
> Besides `ssh-copy-id`, you can directly edit `~/.ssh/authorized_keys` to add new public key for a client.

## Create Key Pair

```sh
ssh-keygen [-t <type>] [-f <path>] [-N <passphrase>] [-C <comment>]
```

- `-t`: specify key type, `ed25519` by default.
- `-f`: output fullname of the key, public key will have extra extension `.pub`. `ssh-keygen` will prompt for it if not specified anyway.
- `-N`: specify passphrase, will ask anyway if unspecified. This may expose your passphrase to command history.
- `-C`: comment for identifying the key.

> [!NOTE]
> see `man ssh-keygen`
## Re-encrypt Key

You might want to encrypt the keys with different type and passphrase, as well as a new name.

```sh
ssh-keygen -p [-t <type>] [-f <path>] [-P <old_passphrase>] [-N <new_passphrase>]
```

Similar to creating a key pair, you don't have to fill all options, leave them empty and let the cli prompt for you.
86 changes: 86 additions & 0 deletions docs/document/Articles/docs/Setup nix-on-droid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Setup nix-on-droid

## Prerequisites

- Root privilege on you android phone(it's not stable in devices without root)
- nix-on-droid installed.
- Optional: `adb`, `pwsh`
You can use `adb` to type inputs from your computer connected with your android device.
The following powershell function solves the escape problem of `adb shell input text`, so you don't have to escape manually.
```ps1
# Use -Enter to press enter after command input
function adbin([string]$Str, [switch]$Enter) {
$special = @( ' ', '\|', '\$', '&', '\(', '\)', '~','\*', "\'",'"','<','>')
foreach ($char in $special) {
$Str = $Str -replace $char, ($char.Length -gt 1 ? $char : "\$char")
}
adb shell input text $Str
if ($Enter) {
adb shell input keyevent KEYCODE_ENTER
}
}
```
> [!NOTE]
> You can wrap the same as bash function by `awk` or other text manipulation tools.
## Init
- nix-on-droid may ask for url for certain file, if the url is not accessible on your phone, download it and transfer to your phone. And replace the default url as `file:///sdcard/...`
- type `yes` when nix prompt for downloads for first init.
- add and update channels:
```sh
nix-channel --add /~https://github.com/nix-community/home-manager/archive/release-24.05.tar.gz home-manager && channel
```
> [!TIP]
> If you use the wrapper function mentioned above, would be like this:
>```ps1
>adbin -Enter 'nix-channel --add /~https://github.com/nix-community/home-manager/archive/release-24.05.tar.gz home-manager'
>```
## Connect to nix-on-droid
- Install `openssh`
```sh
nix profile install nixpkgs#openssh
```

- create a empty `ssh_config`, `sshd` requires at least one specified. We don't specify any option in it in this guide but it's needed afterward.

```sh
mkdir -p /etc/ssh/ && touch /etc/ssh/sshd_config
```

- generate a host key for nix-on-droid, change the key type and passphrase as you like, they don't make too much difference.

```sh
ssh-keygen -t rsa -b 4096 -f ssh_host_rsa_key -N "" # key is generated in pwd
```

- create `~/.ssh/authorized_keys` and paste your public key from your computer(`gc ~/.ssh/<name>.pub`) to this file.

```sh
mkdir -p ~/.ssh/ && touch ~/.ssh/authorized_keys && echo <pub> >> ~/.ssh/authorized_keys
```

- start ssh daemon by `sshd`

```sh
sshd -p <port> -h <host_key> -d
```

`-d` is essential to know whether your port is been taken or not. See details in `man sshd`.

- now connect to nix-on-droid from your computer

```ps1
ssh -l nix-on-droid -p <port> <ipaddr_of_phone>
```

> [!NOTE]
> `<ipaddr_of_phone>` can be inspected from your `Settings - About phone`
## Final Step

Finally you can type everything in your computer through SSH! So use nix as you like.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Create Item
# Manipulate Item
## Create Item

Powershell uses a single cmdlet named `New-Item` to represent all kinds of creation logic for file system items like folder, file, symlinks...

Expand All @@ -24,7 +25,7 @@ New-Item <file_path>
> [!TIP]
> Use `-Force` flag to overwrite existing target.
## Directory
### Directory

```ps1
New-Item <dir_path> -ItemType Directory
Expand All @@ -41,19 +42,38 @@ mkdir <dir_path>
> [!TIP]
> Use `-Force` flag to overwrite existing target.
## Symbolic Link
### Symbolic Link

```ps1
New-Item <symlink_path> -Target <source> -ItemType SymbolicLink
```

> [!NOTE]
> `-Target` is an alias of `-Value`
> [!TIP]
> Use `-Force` flag to overwrite existing target.
## Ignore Wildcards
### Ignore Wildcards

`-Path` translates wildcards by default, if you do need to include special characters from wildcards syntax for your new item, use `-LiteralPath`.

```ps1
New-Item -LiteralPath 'foo*.txt' # creates a file literally named `foo*.txt`
```

## Delete

- delete file

```ps1
ri <file>
```

- delete folder

```ps1
ri -rec -force <folder>
```

### Rename

This file was deleted.

23 changes: 23 additions & 0 deletions docs/document/PowerShell/docs/File System/6.Inspect File System.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Inspect File System

## List Items

- Recursively

```ps1
gci -rec
```

- Include Hidden Items

```ps1
gci -force
```

## Size

- Directory Size

```ps1
gci -file -rec -force | measure { $_.Length / 1MB } -Sum
```
3 changes: 3 additions & 0 deletions docs/document/PowerShell/docs/Language/HashTable.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ $foo['Name'] # foo
$foo['Name', 'Age'] # @('foo', 18)
```
> [!NOTE]
> PowerShell uses `TryGetValue` when accessing value with indexer syntax for `IDictionary` types, so it never throw.
`.` accessor would also works **as long as there's no duplicated Extended Property with the same name of the key you passed.**

```ps1
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ gci | measure -Property Length -Sum -Max
# -Property is positional
gci | measure Length -Sum -Max
# measure different properties with same rule
gps | measure VM,ID -Sum -Max
# Calculate Max in unit GB
gci | measure { $_.Length / 1GB } -Max
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,15 @@ gci -file | where Extension -eq '.ps1'
gci -file | where { $_.Extension -eq '.ps1' }
```

> [!NOTE]
> See: `help where`
## Intrinsic Where

Intrinsic `Where` can be useful when performance matters, it provides a way to return early base on certain condition without consuming the whole iteration.

- First or Last items satisfy certain condition
- Skip until one satisfies the condition and return all remaining items(including the one satisfies)
- Return items until one not satisfies the condition(excluding the one satisfies)
- Split items into two collections, one contains items satisfied the condition, the another are items remained.

```cs
Where(scriptblock condition, WhereOperatorSelectionMode mode = 0, int? count)
```
3 changes: 3 additions & 0 deletions docs/document/PowerShell/docs/Object Manipulation/7.Group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Group

- Group by property by `-Property`
21 changes: 0 additions & 21 deletions docs/document/PowerShell/docs/Object Manipulation/ETS Property.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Extended Type System(ETS) is for consistent experience with **some** `.NET` type

There's two approaches that PowerShell did for implementing the ETS.

- Intrinsic members for all objects.
- Intrinsic members for all objects, and intrinsic member for specific type of objects.
- Dynamic manipulation to members of an object.
- Potentially wrap an object as `PSObject`.

Expand Down Expand Up @@ -40,11 +40,16 @@ Intrinsic methods and properties are to mimic singular object and collection in
- `Count`
- `Length`

### Constructor

For all objects of `System.Type`, there's a intrinsic `New` method as constructor.

> [!NOTE]
>**Intrinsic members are not described as part of certain type definition, they're isolated from the object-oriented type system.**
> Object views are visible to `Get-Member -Force` while intrinsic methods aren't.


A common example would be `Process` type.

```ps1
Expand Down
Loading

0 comments on commit 4cec095

Please sign in to comment.