wsl2 端口映射

把所有地址的 80 端口监听到本地 80,局域网可以通过 80 访问 wsl2

1
2
3
netsh interface portproxy add v4tov4 listenaddress=* listenport=80 connectaddress=127.0.0.1 connectport=80 protocol=tcp

netsh interface portproxy delete v4tov4 listenport=80 protocol=tcp

切换默认 root 用户

找到 ubuntu 安装目录,一般在
C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu18.04onWindows_1804.2019.522.0_x64__79rhkp1fndgsc
文件夹下面。

1
2
3
4
./ubuntu1804.exe config --default-user root

# 重启WSL服务
net stop LxssManagernet start LxssManager

git

git config --global http.proxy 127.0.0.1:1080

git config --global https.proxy 127.0.0.1:1080

git config --global http.proxy 'socks5://127.0.0.1:1080'

git config --global https.proxy 'socks5://127.0.0.1:1080'

取消代理:

git config --global --unset http.proxy

git config --global --unset https.proxy

查看当前已设置的代理:

git config --global --get http.proxy

git config --global --get https.proxy

wsl

win 应用商店安装 unbuntu

找到 ubuntu 安装目录,一般在C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu18.04onWindows_1804.2019.522.0_x64__79rhkp1fndgsc文件夹下面。

在文件夹中打开 powershell 运行下面的命令:
./ubuntu.exe config --default-user root

切换 apt 源为清华源

https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/

sudo apt-get update

zsh

sudo apt-get install -y zsh

oh-my-zsh

wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O - | sh

  1. 在以 root 用户为前提下,oh-my-zsh 的安装目录:/root/.oh-my-zsh
  2. 在以 root 用户为前提下,Zsh 的配置文件位置:/root/.zshrc
  3. 为 root 用户设置 zsh 为系统默认 shell:chsh -s /bin/zsh root
  4. 如果你要重新恢复到 bash:chsh -s /bin/bash root

终端 Hyper

字体 等距更纱黑体 T SC

主题 bullet-train

下载 bullet-train.zsh-theme放入
root\.oh-my-zsh\custom\themes

1
2
// .zshrc
ZSH_THEME="bullet-train"

插件

autojump

sudo apt-get install autojump

1
2
// .zshrc配置文件的最后一行加上
. /usr/share/autojump/autojump.sh

source ~/.zshrc重新编译 zsh

highlighting

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

~/.zshrc

plugins=(其他的插件 zsh-syntax-highlighting)

autosuggestions

1
2
3
4
5
6
7
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions


~/.zshrc
ZSH_DISABLE_COMPFIX=true // 第一行

plugins=( ... zsh-autosuggestions)

安装 nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

.zshrc

1
2
3
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

npm

设置代理
npm config set https-proxy http://127.0.0.1:1080

npm config set proxy http://127.0.0.1:1080

npm install -g cnpm --registry=https://registry.npm.taobao.org

开启关闭 Hyper-V

bcdedit /set hypervisorlaunchtype off

bcdedit /set hypervisorlaunchtype auto

代理

可以共用 windows 系统的代理,比如在 windows 下运行了 ss 客户端后,可以设置命令别名:

1
2
alias proxy="export ALL_PROXY=socks5://127.0.0.1:1080"
alias unproxy="unset ALL_PROXY"

new .zshrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# correct white list
alias vim='nocorrect vim'
alias yrm='nocorrect yrm'


#!usr/bin/env zsh

[ -z "$(ps -ef | grep cron | grep -v grep)" ] && sudo /etc/init.d/cron start &> /dev/null
export ELECTRON_MIRROR="https://npm.taobao.org/mirrors/electron/"

# - - - - - - - - - - - - - - - - - - - -
# Profiling Tools
# - - - - - - - - - - - - - - - - - - - -

PROFILE_STARTUP=false
if [[ "$PROFILE_STARTUP" == true ]]; then
zmodload zsh/zprof
# http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
PS4=$'%D{%M%S%.} %N:%i> '
exec 3>&2 2>$HOME/startlog.$$
setopt xtrace prompt_subst
fi


# - - - - - - - - - - - - - - - - - - - -
# Instant Prompt
# - - - - - - - - - - - - - - - - - - - -

# Enable Powerlevel10k instant prompt. Should stay close to the top of `~/.zshrc`.
# Initialization code that may require console input ( password prompts, [y/n]
# confirmations, etc. ) must go above this block, everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi


# - - - - - - - - - - - - - - - - - - - -
# Homebrew Configuration
# - - - - - - - - - - - - - - - - - - - -

# If You Come From Bash You Might Have To Change Your $PATH.
# export PATH=:/usr/local/bin:/usr/local/sbin:$HOME/bin:$PATH
export PATH="$HOME/bin:/usr/local/bin:$PATH"


# - - - - - - - - - - - - - - - - - - - -
# Zsh Core Configuration
# - - - - - - - - - - - - - - - - - - - -

# Install Functions.
export XDG_CONFIG_HOME="$HOME/.config"
export UPDATE_INTERVAL=15

export DOTFILES="$HOME/dotfiles"
export ZSH="$HOME/dotfiles/zsh"

export CACHEDIR="$HOME/.local/share"
[[ -d "$CACHEDIR" ]] || mkdir -p "$CACHEDIR"

# Load The Prompt System And Completion System And Initilize Them.
autoload -Uz compinit promptinit

# Load And Initialize The Completion System Ignoring Insecure Directories With A
# Cache Time Of 20 Hours, So It Should Almost Always Regenerate The First Time A
# Shell Is Opened Each Day.
# See: https://gist.github.com/ctechols/ca1035271ad134841284
_comp_files=(${ZDOTDIR:-$HOME}/.zcompdump(Nm-20))
if (( $#_comp_files )); then
compinit -i -C
else
compinit -i
fi
unset _comp_files
promptinit
setopt prompt_subst


# - - - - - - - - - - - - - - - - - - - -
# ZSH Settings
# - - - - - - - - - - - - - - - - - - - -

autoload -U colors && colors # Load Colors.
unsetopt case_glob # Use Case-Insensitve Globbing.
setopt globdots # Glob Dotfiles As Well.
setopt extendedglob # Use Extended Globbing.
setopt autocd # Automatically Change Directory If A Directory Is Entered.

# Smart URLs.
autoload -Uz url-quote-magic
zle -N self-insert url-quote-magic

# General.
setopt brace_ccl # Allow Brace Character Class List Expansion.
setopt combining_chars # Combine Zero-Length Punctuation Characters ( Accents ) With The Base Character.
setopt rc_quotes # Allow 'Henry''s Garage' instead of 'Henry'\''s Garage'.
unsetopt mail_warning # Don't Print A Warning Message If A Mail File Has Been Accessed.

# Jobs.
setopt long_list_jobs # List Jobs In The Long Format By Default.
setopt auto_resume # Attempt To Resume Existing Job Before Creating A New Process.
setopt notify # Report Status Of Background Jobs Immediately.
unsetopt bg_nice # Don't Run All Background Jobs At A Lower Priority.
unsetopt hup # Don't Kill Jobs On Shell Exit.
unsetopt check_jobs # Don't Report On Jobs When Shell Exit.

# setopt correct # Turn On Corrections

# Completion Options.
setopt complete_in_word # Complete From Both Ends Of A Word.
setopt always_to_end # Move Cursor To The End Of A Completed Word.
setopt path_dirs # Perform Path Search Even On Command Names With Slashes.
setopt auto_menu # Show Completion Menu On A Successive Tab Press.
setopt auto_list # Automatically List Choices On Ambiguous Completion.
setopt auto_param_slash # If Completed Parameter Is A Directory, Add A Trailing Slash.
setopt no_complete_aliases

setopt menu_complete # Do Not Autoselect The First Completion Entry.
unsetopt flow_control # Disable Start/Stop Characters In Shell Editor.

# Zstyle.
zstyle ':completion:*:*:*:*:*' menu select
zstyle ':completion:*:matches' group 'yes'
zstyle ':completion:*:options' description 'yes'
zstyle ':completion:*:options' auto-description '%d'
zstyle ':completion:*:corrections' format ' %F{green}-- %d (errors: %e) --%f'
zstyle ':completion:*:descriptions' format ' %F{yellow}-- %d --%f'
zstyle ':completion:*:messages' format ' %F{purple} -- %d --%f'
zstyle ':completion:*:warnings' format ' %F{red}-- no matches found --%f'
zstyle ':completion:*:default' list-prompt '%S%M matches%s'
zstyle ':completion:*' format ' %F{yellow}-- %d --%f'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' verbose yes
zstyle ':completion::complete:*' use-cache on
zstyle ':completion::complete:*' cache-path "$HOME/.zcompcache"
zstyle ':completion:*' list-colors $LS_COLORS
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01'
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*:functions' ignored-patterns '(_*|pre(cmd|exec))'
zstyle ':completion:*' rehash true

# History.
HISTFILE="${ZDOTDIR:-$HOME}/.zhistory"
HISTSIZE=100000
SAVEHIST=5000
setopt appendhistory notify
unsetopt beep nomatch

setopt bang_hist # Treat The '!' Character Specially During Expansion.
setopt inc_append_history # Write To The History File Immediately, Not When The Shell Exits.
setopt share_history # Share History Between All Sessions.
setopt hist_expire_dups_first # Expire A Duplicate Event First When Trimming History.
setopt hist_ignore_dups # Do Not Record An Event That Was Just Recorded Again.
setopt hist_ignore_all_dups # Delete An Old Recorded Event If A New Event Is A Duplicate.
setopt hist_find_no_dups # Do Not Display A Previously Found Event.
setopt hist_ignore_space # Do Not Record An Event Starting With A Space.
setopt hist_save_no_dups # Do Not Write A Duplicate Event To The History File.
setopt hist_verify # Do Not Execute Immediately Upon History Expansion.
setopt extended_history # Show Timestamp In History.


# - - - - - - - - - - - - - - - - - - - -
# Zinit Configuration
# - - - - - - - - - - - - - - - - - - - -

__ZINIT="${ZDOTDIR:-$HOME}/.zinit/bin/zinit.zsh"

if [[ ! -f "$__ZINIT" ]]; then
print -P "%F{33}▓▒░ %F{220}Installing DHARMA Initiative Plugin Manager (zdharma/zinit)…%f"
command mkdir -p "$HOME/.zinit" && command chmod g-rwX "$HOME/.zinit"
command git clone https://github.com/zdharma-continuum/zinit "$HOME/.zinit/bin" && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The clone has failed.%f%b"
fi

. "$__ZINIT"
autoload -Uz _zinit
(( ${+_comps} )) && _comps[zinit]=_zinit


# - - - - - - - - - - - - - - - - - - - -
# Theme
# - - - - - - - - - - - - - - - - - - - -

# Most Themes Use This Option.
setopt promptsubst

# These plugins provide many aliases - atload''
zinit wait lucid for \
OMZ::lib/git.zsh \
atload"unalias grv" \
OMZ::plugins/git/git.plugin.zsh

# Provide A Simple Prompt Till The Theme Loads
PS1="READY >"
zinit ice wait'!' lucid
zinit ice depth=1; zinit light romkatv/powerlevel10k


# - - - - - - - - - - - - - - - - - - - -
# Annexes
# - - - - - - - - - - - - - - - - - - - -

# Load a few important annexes, without Turbo (this is currently required for annexes)
zinit light-mode compile"handler" for \
zdharma-continuum/z-a-patch-dl \
zdharma-continuum/z-a-as-monitor \
zdharma-continuum/z-a-bin-gem-node \
zdharma-continuum/z-a-submods \
zdharma-continuum/declare-zsh


# - - - - - - - - - - - - - - - - - - - -
# Plugins
# - - - - - - - - - - - - - - - - - - - -

zinit wait lucid light-mode for \
OMZ::lib/compfix.zsh \
OMZ::lib/completion.zsh \
OMZ::lib/functions.zsh \
OMZ::lib/diagnostics.zsh \
OMZ::lib/git.zsh \
OMZ::lib/nvm.zsh \
OMZ::lib/grep.zsh \
OMZ::lib/key-bindings.zsh \
OMZ::lib/misc.zsh \
OMZ::lib/spectrum.zsh \
OMZ::lib/termsupport.zsh \
OMZ::plugins/git-auto-fetch/git-auto-fetch.plugin.zsh \
atinit"zicompinit; zicdreplay" \
zdharma-continuum/fast-syntax-highlighting \
OMZ::plugins/colored-man-pages/colored-man-pages.plugin.zsh \
OMZ::plugins/command-not-found/command-not-found.plugin.zsh \
atload"_zsh_autosuggest_start" \
zsh-users/zsh-autosuggestions \
as"completion" \
OMZ::plugins/docker/_docker \
OMZ::plugins/composer/composer.plugin.zsh \
OMZ::plugins/thefuck/thefuck.plugin.zsh \
# htlsne/zplugin-rbenv \
# OMZ::plugins/pyenv/pyenv.plugin.zsh

# Easy Way ※If you don't mind light errors during installation in completion.zsh
zinit ice atclone'./init.sh' nocompile'!' wait'!0'

# Recommended Be Loaded Last.
zinit ice wait blockf lucid atpull'zinit creinstall -q .'
zinit load zsh-users/zsh-completions
zinit load agkozak/zsh-z

# Semi-graphical .zshrc editor for zinit commands
zinit load zdharma-continuum/zui
zinit ice lucid wait'[[ -n ${ZLAST_COMMANDS[(r)cras*]} ]]'
zinit load zdharma-continuum/zplugin-crasis


# - - - - - - - - - - - - - - - - - - - -
# User Configuration
# - - - - - - - - - - - - - - - - - - - -

setopt no_beep

# - - - - - - - - - - - - - - - - - - - -
# cdr, persistent cd
# - - - - - - - - - - - - - - - - - - - -

autoload -Uz chpwd_recent_dirs cdr add-zsh-hook
add-zsh-hook chpwd chpwd_recent_dirs
DIRSTACKFILE="$HOME/.cache/zsh/dirs"

# Make `DIRSTACKFILE` If It 'S Not There.
if [[ ! -a $DIRSTACKFILE ]]; then
mkdir -p $DIRSTACKFILE[0,-5]
touch $DIRSTACKFILE
fi

if [[ -f $DIRSTACKFILE ]] && [[ $#dirstack -eq 0 ]]; then
dirstack=( ${(f)"$(< $DIRSTACKFILE)"} )
fi

chpwd() {
print -l $PWD ${(u)dirstack} >>$DIRSTACKFILE
local d="$(sort -u $DIRSTACKFILE )"
echo "$d" > $DIRSTACKFILE
}

DIRSTACKSIZE=20

setopt auto_pushd pushd_silent pushd_to_home

setopt pushd_ignore_dups # Remove Duplicate Entries
setopt pushd_minus # This Reverts The +/- Operators.


# - - - - - - - - - - - - - - - - - - - -
# Theme / Prompt Customization
# - - - - - - - - - - - - - - - - - - - -

# To Customize Prompt, Run `p10k configure` Or Edit `~/.p10k.zsh`.
[[ ! -f ~/.p10k.zsh ]] || . ~/.p10k.zsh


# - - - - - - - - - - - - - - - - - - - -
# End Profiling Script
# - - - - - - - - - - - - - - - - - - - -

if [[ "$PROFILE_STARTUP" == true ]]; then
unsetopt xtrace
exec 2>&3 3>&-
zprof > ~/zshprofile$(date +'%s')
fi

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# nvm
export NVM_DIR="$HOME/.config/nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

export GODEBUG=x509ignoreCN=0
export PATH="$HOME/.local/bin:$PATH"%


### proxy start
# 获取 windows 的 ip
winip=$(ip route | grep default | awk '{print $3}')

# 我使用的是 clash,并且开启了局域网访问
PROXY_HTTP="http://${winip}:7890"
PROXY_SOCKS5="socks5://${winip}:7890"
PROXY_HTTP_IP="${winip}:7890"
PROXY_SOCKS5_IP="${winip}:7890"


# 查看我的 ip
ip_() {
echo "WIN ip: ${winip}"
echo ""
echo "使用以下指令"
echo "proxy_npm"
echo "unproxy_npm"
echo "proxy_git"
echo "proxy_global_git"
echo "unproxy_global_git"
echo "proxy"
echo "unproxy"

}

# 设置 npm 代理,但是一般来说将 npm 换成淘宝镜像就没什么问题了
proxy_npm() {
npm config set proxy ${PROXY_HTTP}
npm config set https-proxy ${PROXY_HTTP}
yarn config set proxy ${PROXY_HTTP}
yarn config set https-proxy ${PROXY_HTTP}
}

unproxy_npm() {
npm config delete proxy
npm config delete https-proxy
yarn config delete proxy
yarn config delete https-proxy
}

proxy_global_git() {
git config --global http.proxy ${PROXY_HTTP}
git config --global https.proxy ${PROXY_HTTP}
}
unproxy_global_git() {
git config --global --unset http.proxy
git config --global --unset https.proxy
}

# 代理 github,因为在国内 clone 的时候很慢
# 先检测 ~/.ssh/config 文件中有没有 github.com 这个域名,有的话就将 ip 换成最新的 ip
proxy_git() {
git config --global http.https://github.com.proxy ${PROXY_HTTP}
if ! grep -qF "Host github.com" ~/.ssh/config ; then
echo "" >> ~/.ssh/config
echo "Host github.com" >> ~/.ssh/config
echo " User git" >> ~/.ssh/config
echo " ProxyCommand nc -X 5 -x ${PROXY_SOCKS5_IP} %h %p" >> ~/.ssh/config
else
lino=$(($(awk '/Host github.com/{print NR}' ~/.ssh/config)+2))
sed -i "${lino}c\ ProxyCommand nc -X 5 -x ${PROXY_SOCKS5_IP} %h %p" ~/.ssh/config
fi
}

# 设置一系列的代理命令
proxy () {
# pip can read http_proxy & https_proxy
export http_proxy="${PROXY_HTTP}"
export HTTP_PROXY="${PROXY_HTTP}"

export https_proxy="${PROXY_HTTP}"
export HTTPS_proxy="${PROXY_HTTP}"

export ftp_proxy="${PROXY_HTTP}"
export FTP_PROXY="${PROXY_HTTP}"

export rsync_proxy="${PROXY_HTTP}"
export RSYNC_PROXY="${PROXY_HTTP}"

export ALL_PROXY="${PROXY_SOCKS5}"
export all_proxy="${PROXY_SOCKS5}"

proxy_git
proxy_npm
proxy_global_git
if [ ! $1 ]; then
ip_
fi
}

unproxy () {
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
unset ftp_proxy
unset FTP_PROXY
unset rsync_proxy
unset RSYNC_PROXY
unset ALL_PROXY
unset all_proxy

echo "取消proxy成功"
echo "可以继续执行"
echo "unproxy_npm"
echo "unproxy_global_git"
}
# proxy end

old .zshrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
ZSH_DISABLE_COMPFIX=true

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion


# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="/root/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="agnoster"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in ~/.oh-my-zsh/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS=true

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git autojump zsh-syntax-highlighting zsh-autosuggestions)

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
# export EDITOR='vim'
# else
# export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

### proxy start
# 获取 windows 的 ip
winip=$(ip route | grep default | awk '{print $3}')
wslip=$(hostname -I | awk '{print $1}')

# 我使用的是 clash,并且开启了局域网访问
PROXY_HTTP="http://${winip}:7890"
PROXY_SOCKS5="socks5://${winip}:7891"
PROXY_HTTP_IP="${winip}:7891"
PROXY_SOCKS5_IP="${winip}:7891"


# 查看我的 ip
ip_() {
curl cip.cc/$1
echo "WIN ip: ${winip}"
echo "WSL ip: ${wslip}"
echo ""
echo "使用以下指令"
echo "proxy_npm"
echo "unproxy_npm"
echo "proxy_git"
echo "proxy_global_git"
echo "unproxy_global_git"
echo "proxy"
echo "unproxy"

}

# 设置 npm 代理,但是一般来说将 npm 换成淘宝镜像就没什么问题了
proxy_npm() {
npm config set proxy ${PROXY_HTTP}
npm config set https-proxy ${PROXY_HTTP}
yarn config set proxy ${PROXY_HTTP}
yarn config set https-proxy ${PROXY_HTTP}
}

unproxy_npm() {
npm config delete proxy
npm config delete https-proxy
yarn config delete proxy
yarn config delete https-proxy
}

proxy_global_git() {
git config --global http.proxy ${PROXY_HTTP}
git config --global https.proxy ${PROXY_HTTP}
}
unproxy_global_git() {
git config --global --unset http.proxy
git config --global --unset https.proxy
}

# 代理 github,因为在国内 clone 的时候很慢
# 先检测 ~/.ssh/config 文件中有没有 github.com 这个域名,有的话就将 ip 换成最新的 ip
proxy_git() {
git config --global http.https://github.com.proxy ${PROXY_HTTP}
if ! grep -qF "Host github.com" ~/.ssh/config ; then
echo "" >> ~/.ssh/config
echo "Host github.com" >> ~/.ssh/config
echo " User git" >> ~/.ssh/config
echo " ProxyCommand nc -X 5 -x ${PROXY_SOCKS5_IP} %h %p" >> ~/.ssh/config
else
lino=$(($(awk '/Host github.com/{print NR}' ~/.ssh/config)+2))
sed -i "${lino}c\ ProxyCommand nc -X 5 -x ${PROXY_SOCKS5_IP} %h %p" ~/.ssh/config
fi
}

# 设置一系列的代理命令
proxy () {
# pip can read http_proxy & https_proxy
export http_proxy="${PROXY_HTTP}"
export HTTP_PROXY="${PROXY_HTTP}"

export https_proxy="${PROXY_HTTP}"
export HTTPS_proxy="${PROXY_HTTP}"

export ftp_proxy="${PROXY_HTTP}"
export FTP_PROXY="${PROXY_HTTP}"

export rsync_proxy="${PROXY_HTTP}"
export RSYNC_PROXY="${PROXY_HTTP}"

export ALL_PROXY="${PROXY_SOCKS5}"
export all_proxy="${PROXY_SOCKS5}"

proxy_git
proxy_npm
proxy_global_git
if [ ! $1 ]; then
ip_
fi
}

unproxy () {
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
unset ftp_proxy
unset FTP_PROXY
unset rsync_proxy
unset RSYNC_PROXY
unset ALL_PROXY
unset all_proxy

echo "取消proxy成功"
echo "可以继续执行"
echo "unproxy_npm"
echo "unproxy_global_git"
}

### proxy end