Code highlighter & Mathjax

顯示具有 Coding 標籤的文章。 顯示所有文章
顯示具有 Coding 標籤的文章。 顯示所有文章

2021年7月30日 星期五

How to set up NAT6 or IPv6 NAT on OpenWrt

IPV6 NAT is not recommended in fact, because the devices under routing can't obtain the IPV6 address of the public network, which will lose the great significance of using IPV6. This is only recommended in the campus network environment. Under the condition of public network home broadband, penetration mode or direct distribution of public IPV6 address through DHCPV6 is recommended.

Here's the documentation how to set up NAT6 or IPv6 NAT on OpenWrt Chaos Calmer:

Prerequesites: This guide assumes that you already have a working IPv6 WAN connection on your OpenWrt router and you want to allow your client devices to use this connection via NAT6.

1) Install the package kmod-ipt-nat6 either via the LuCI interface under "System" -> "Software" or via ssh with the command

opkg update && opkg install kmod-ipt-nat6

2) In the LuCI webinterface go to "Network" -> "Interface". Change the first letter of the "IPv6 ULA Prefix" from 'f' to 'd'.

Explanation: If you do not do this, IPv6 NAT may still work on some clients, but others will prefer the IPv4 route instead because the default prefix (starting with something like fd25...) does not indicate a globally routed address. Changing this to a global IPv6 address solves this - just make sure it's an address that is not being used yet (addresses starting with d... are unassigned and therefore safe to use).


3) On the same page, move up to the "LAN" section and click on "Edit". There you scoll down to "DHPC Server" and hit the tab "IPv6 Settings". Then check the box "Always announce default router".


4) Make sure that the following values are set (should be by default): "Router Advertisement-Service" and "DHCPv6-Service" should both be set to "server mode", "NDP-Proxy" to "disabled" and "DHCPv6-Mode" to "stateless + stateful".

Note: The original blog post on which this guide is based on recommends to disable the DHCPv6-Service. But my testing showed that with DHCPv6 disabled, some clients would still prefer IPv4 even though IPv6 worked as well (e.g. my Android smartphone showed this behaviour). Enabling DHCPv6 solved this.


5) Last but not least, you need a small script to add the actuall IPv6 NAT rule to the firewall and set the default route/gateway. Via ssh create a new file /root/nat6.sh with the following content (using your favorite editor like vi, nano, etc.):


#/bin/ash

# Wait until IPv6 route is up...
# Don't loop infinitely, but stop after (delay x limit) seconds
line=0
count=1
delay=5
limit=24
while [ $line -eq 0 ]
do
    if [ $count -gt $limit ]
    then
        exit 1
    fi
    sleep $delay
    count=$((count+1))
    line=`route -A inet6 | grep ::/0 | awk 'END{print NR}'`
done

# Add masquerading rule (NAT6) to the firewall
ip6tables -t nat -I POSTROUTING -s `uci get network.globals.ula_prefix` -j MASQUERADE

# Set default gateway for requests to global addresses
route -A inet6 add 2000::/3 `route -A inet6 | grep ::/0 | awk 'NR==1{print "gw "$2" dev "$7}'`

# Set accept_ra to 2, otherwise temporary addresses won't work
echo 2 > /proc/sys/net/ipv6/conf/`route -A inet6 | grep ::/0 | awk 'NR==1{print $7}'`/accept_ra

# Use temporary addresses (IPv6 privacy extensions)
echo 2 > /proc/sys/net/ipv6/conf/`route -A inet6 | grep ::/0 | awk 'NR==1{print $7}'`/use_tempaddr
Note: If you do not want or cannot use IPv6 privacy extensions on your OpenWrt router, you can remove the last 5 lines (starting from "# Set accept_ra to 2...") from this script. Then your IPv6 suffix or interface identifier will be static.

6) Make the script executable by issuing the following command via ssh:
chmod +x /root/nat6.sh

7) Make the script being executed whenever your router boots. To do this, add the folloing line to /etc/rc.local before the last line that contains 'exit 0':
/root/nat6.sh &
You can do this either via ssh using your preferred editor or via LuCI under "System" -> "Startup" -> "Local Startup".

8) Restart your router and verify IPv6 is working on your clients.

Credits
This solution was based on this blog post: http://blog.iopsl.com/ipv6-nat-with-openwrt-router/


2019年2月11日 星期一

Fuck Off Bad Commit Messages


We’ve All Seen It…

You’re working on a project and it uses Git for version control.

You’ve just finished making a change, and you want to quickly update your branch.

So, you open up your terminal, and with a few quick commands, you update your remote branch with your changes.

git add .
git commit -m "added new feature"  
git push  

But then you do a bit of testing and find that you have a bug in your implementation.
No worries — you quickly find a fix and make another commit to fix the problem.
git add .
git commit -m "fix bug"
git push
You repeat this process a few times, and now you end up with a git commit log that looks like:




At the moment, this seems fine to you.
After all, you just worked on it, and you can easily explain what was worked on — even if the messages don’t clearly convey it. The Problem 

A few months pass, and now, another developer is looking back through the changes you made.
They try to understand the high-level details of your changes, but since the commit messages are not descriptive, they cannot glean any information.
They then resort to reading through each commit’s diff. However, even after doing so, they still cannot identify the thought process behind the choices that you made in your implementation.
Now, since software engineering is a collaborative process and the git blame operation exists, they find out who made these changes and start asking you questions about your implementation.
However, since it was so long ago, you don’t remember much. You check back through your commits, and you no longer remember the logic behind the implementation decisions made in that project.
You send your colleague a sad emoji on Slack (😔) and tell them that you can’t provide any more information than what they already have.


Writing Good Commit Messages 

Hopefully, the above situation has demonstrated why it is important to write good, informative git commit messages.
In a field as collaborative as software engineering, it is imperative that we make it easy for collaborators to quickly gain context into our work.
Ideally, a good commit message will be structured into three parts — the subject, the body, and the closing line.
Subject line
The subject should be a single line that summarizes your commit’s changes.
It should be written in the imperative tense, begin with a capital letter, not end with a period, and be 50 characters or less.
A good subject line will complete the sentence “This commit will …”.
A good commit message, like “add new neural network model to back-end”, nicely finishes the sentence.
A bad commit message, such as “fix bug”, does not complete the sentence very nicely, producing the awkward sentence “This commit will fix bug”.
Body
The body contains the meat of your message and is where you can go into details regarding your changes. Note that for some very small commits, such as fixing a typo, you probably won’t need a body, as the subject line should be informative enough.
In the body, you should go into more details about the changes you are making, and explain the context of what you are doing.
You can explain why you are making these changes, why you are choosing to implement the changes in this particular way, and anything else that would help people understand the thought process behind your commit.
Try not to repeat things that are obvious from the code changes in the diff. There is no need to provide a line-by-line explanation of your changes. Focus on covering more high-level details that may not be obvious from reading the code. The goal is ultimately to provide context into the development process around this change, which primarily concerns its motivations and goals.
Closing line
Finally, the closing line is the last line of your commit message.
This is where you can put useful meta-data regarding your commit, such as JIRA ticket numbers, GitHub issue numbers, co-author names, and additional links.
This can help to link important information together that relates to your change.

2018年6月10日 星期日

How I Became A Keyboard Warrior(How I stopped using mouse)



I’m sure you have seen this interesting rift between programmers that goes beyond the friendly competition about who favors which IDE or which programming language has the nicer syntax — the rift that extends to the very core of how we navigate the systems in front of us.

In essence, there are two kinds of people when it comes to computer navigation: Those who rely on the mouse and can’t understand why anyone would rather type text and on the other side, there’s us few who have seen the light and prefer using the keyboard as much as possible. Here are some good reasons.




Keyboard is MUCH faster (Once you get used to it)

If I need to convince you, let me give you the easiest of examples that probably everyone understands: Control-C/V instead of reaching for the mouse, right-clicking, finding the right menu item in a million of choices, then doing it all again for the pasting. Nearly everyone I know has long understood to use the keyboard for copy/pasting, even the non-IT staff.

But this is just the tip of the iceberg, and most people still need the mouse to select the text in the first place (instead of using control-shift-arrows or shift-end/Home). Most people also backspace individual typos instead of control-backspacing and retyping the whole word. Many also use the mouse to switch between active windows instead of alt-tabbing.

It is hard to explain to people how something so simple as using the mouse wastes a lot of time that most people don’t even recognize. It seems like no time at all, but I can guarantee you that most people waste an hour or even more every single day this way. Wouldn’t you rather do something else with that time, either slack off or get more work done?
Navigating text with your keyboard

For how simple it is a surprising amount of people do not make use of the basic, default options that your computer gives you: Navigating text efficiently.

Almost every text editor allows you to do these simple things that still save you hours of time over the course of your life:
Control-Backspace / Control-Delete will eradicate the left/right word without the need to individually backspace each letter.
Shift plus your arrow keys lets you select text with the keyboard, add control in there and you can select whole words. Use shift and Home/End to select a whole line, use them without shift to jump to the end or start of a line. This way you can press Home-Shift-End-Backspace and a whole line is gone in a second.
Control-UpArrow will jump up a Paragraph in many text editors, something that I don’t use much but it’s a little faster than using just the UpArrow.
Control-F allows you to find words, it also allows to quickly jump down to a specific part of a website or long document when you know what you are searching for. Control-H lets you open the same window and replace words / phrases in most pograms.
Control-Home/End allow you to jump to the beginning/end of the document which is very useful if you went back to correct a sentence or something and then want to continue writing at the end of the document.

Navigating your browser (for beginners)


Almost everyone I know relies on the mouse to use their browsers, it seems like hardly everyone even knows you can use Control-L to jump to the search / URL bar and type up a website URL.
Control-T will open a new tab
Control-W will close the current tab.
Control-1 through 8 will cycle through open tabs, control-9 will always jump to the last open tab to the very right.
Control-PageUp/Down will cycle through your open tabs, just PageUp/Down will allow you to scroll the page.
Speaking of scrolling the page: You can scroll down by just pressing the space bar which is easily the most convenient way to scroll down while your other hand is busy…holding the water bottle that you use to hydrate well enough and be more responsible than your peers.
Control-R reloads the page and is a little easier to reach than F5 which does the same thing.
Your URL bar is also your search bar, if you type www.google.com into your search bar to then reach for the mouse, click the already open search bar, then type the word you were actually looking for please just stop doing that.

Navigating your browser (advanced)


This requires you to get a little (chrome) plugin called Vimium and it’s the best thing since sliced bread.

It allows you to navigate, scroll, find and click links on the site all without using your mouse. If you are aware of how multi-shortcuts work in IDEs like VS/VSCode/Jetbrains then it will be second nature to you, if not it’s quite easy to get the hang of it:

A shortcut like “Ctrl-KD” means you will press control, keep it down, then press K, then D and your code will be beautifully formatted if you happen to be in Visual Studio. This is confusing at first, but it’s the greatest thing that ever happened in my life because it means that almost all functions in most programs I use now come with shortcuts and I can remember those I need most frequently. Then, if I really don’t remember something I can always go back to using the mouse, aimlessly searching through windows, menu bars, option choices until I figure out where that stupid little function is hiding.

With Vimium you simply press “F” and the page looks like this:











Then you can simply take a look at the link you want to open, press “P” to find out why your wife has grown all cold with you and why she is much nicer to the neighbor who knows how to fix actual real life things and probably can’t use a keyboard as well as you can.

There are other great shortcuts, but this combined with the beginner section is what I use the most. The other one is that j/k will allow you to smoothly scroll up and down the page, that’s quite nice whenever the space bar is too fast and too furious.

Your Windows Explorer is just like a web browser


One thing that many people don’t realize is that your Windows Explorer can be navigated just fine with the keyboard as well.
WindowsKey-E opens the thing.


Using tab, shift, arrow keys you can select files, delete them, use F2 to rename.
With those out of the way it’s time to become a console wizard


I don’t know if you ever had the luxury of watching a console wizard in action, those people who will randomly say things like “yeah, just cd into that folder and run the build.ps1 and the errors should resolve”.


They do not quite grasp why that sounds confusing to many people, even hardened developers. I mean, if you don’t cd into that folder, then how do you even work?


A pipe isn’t something you light after work, you use it during work to chain up commands. Sure, Git has some GUI editors but really why would you? Just open up a powershell or cmd, type git commit and don’t forget to add a descriptive commit message like “fixed the bug”.


If you are in VS Code you get an integrated terminal which sounds pretty useless until you start using it. I’m on a German keyboard layout so it takes just a press of Control-Ö to open it, easy to remember as the German word for terminal is Ökonsole. It’s very environmentally friendly, Control-P will open the command “P”alette where you can quickly access all common features of VS Code.


Tab will serve as an auto-complete for console commands, pressing tab multiple times toggles between the options and shift-tab goes back an option if you were thinking too quickly again and need to backtrack a little.


Using the UpArrow gives you the last used command so you can fix your stupid typo and if you accidentally committed your changes to the master branch you can just use git -unfuck -everything and it will all work without any rebasing or merging.
Avoid preaching, anyone who’s ready to convert will come and ask


The simple truth about life in general, keyboard usage, in particular, is this: Your words won’t change anyone’s opinion. If you read this far it’s not because of my words, but because you previously watched someone do keyboard wizardry and realized how quick it can be.


You were already curious and all I did was to show you the way, lead you astray and now it’s happened and you don’t look at the other gender with the same eyes anymore. They don’t react to your touches the way your keyboard does, they don’t understand you the way your computer does.


Congratulations, you have completed the journey, and even if your beard isn’t grey yet or you can’t even grow one you have achieved sufficient wisdom to be called a Greybeard. Go live in a mountain somewhere and wait until those who seek wisdom come to you.